Winform application freeze - c#

I have Winform application,which does some operation in loops (file access, copy file, ....). During theses operation, the application freeze completely, the job is done but impossible to move the main window or refresh the RichTextBox information (we display errors and the job in progress).
Do you have an idea how to do this ?
Thanks,

Consider using BackgroundWorker.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
It can both run your code in the background and report progress back to to the main
UI thread.

i suggest that you use a Background Worker and let the worker do the loops. i think your application read a file by line and added to the rich textbox control right?
this article will guide you in using BackGround Worker. or this

You can create separate thread and execute other task or may use background worker thread.

Related

Winform application freezes

I'm new to developing Winform application (using C# .NET)
My application will freeze and sometimes even showing "not responding" when there are too many operations running behind, especially when it involves a lot of data reading/writing to/from database. The application did NOT crash though, it just freeze until the all code behind were finish executing.
My question is, how do I "unfreeze" the application. For example, the user can still click the "cancel" button to terminate whatever the operation is running, or show a progress bar or something like that?
Your application freezes because it has single thread, which is responsible both for drawing UI and doing other 'heavy' operations (reading/writing to database). When thread is busy with database, it cannot refresh UI. You should perform 'heavy' operations in background thread(s). Thus main thread will be responsible only for refreshing UI and it will be always ready to do that. Use BackgroundWorker component to run some operations on background threads.
Suggested reading: BackgroundWorker Class Sample for Beginners and How to: Use a Background Worker
A windows application will stop working if you are using a System.Timers.Timer.
To fix this, change System.Timers.Timer by System.Windows.Forms.Timer
Greetings

Gif in Windows Form

I'm building a Windows Form application that manipulates a text file, the manipulation process takes time, so I creadted a Hidden PictureBox Control and I put an animated gif Image in it (Like a Progress Bar), and once the process starts I show the PictureBox, but the gif doesn't move cause it is on the same process I guess, is there any kind of solution...
Thanks
You should be doing your long processing in another thread, or simply by using a Background Worker.
msdn: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Plus, you can report any progress made to a progress bar...
Here's an example of use http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
Note that one of the main reasons why your GUI won't update when you're doing heavy computing, is the fact that the computing is done on the same thread as the UI in running on.
One solution is to use Threads, you fired tle process in one thread, then you show the Image... Here you have an excelent guide for beginners in Threads, Beginners Guide to Threading.
But onther easy solution, is to show the picturebox before the process start, so the picture shows in your form before the process take the control of the main Thread.

C# Window gets blank when busy -> Progressbar? But how?

I have a program which does some copy jobs (via File.Copy) that could last a few minutes. When the user brings another window to foreground in this time the program window gets blank since it doesn't redraw itself.
Now I want to show a ProgressBar in an extra window in the center of the screen, but that window get's blank too.
So I startet it in another thread which didn't help.
I'm quite sure someone did this before but I cannot find a working example. Any Ideas?
There is nice example in the documentation on BackgroundWorker class.
ChrisF is correct. Your long operation should be done in a BackgroundWorker thread. You can use BackgroundWorker to report progress, and hook that up to a progress bar on your form.
You need to thread your operation using a BackgroundWorker. There are other ways to thread the operation, but this one is probably the simplest, and will continue to perform message pumping in the foreground so Windows doesn't think your application has stopped responding.
Another option would be to use a Thread, and use Thread.Join to wait for the background task to complete, since Thread.Join also sends standard message pump information to Windows while it waits.
You can use BackgroundWorker class.
See this answer.

How to show progress status for a long-time-consuming function?

I have a windows form simply like this: 1) a button when clicked will perform an operation taking a long time to complete, 2) a label showing how much percentage of the progress is going on.
In the long operation I mentioned, I write the code to update the Text property of the label but it doesn't work!
Please help me to show the progress status correctly.
You can take a look at the BackgroundWorker class (see the MSDN overview). It allows you to run some long-running operation in background and report progress updates (percentage) and completion from the background task to the user interface. Note that you'll need to calculate the progress percentage yourself.
However, the BackgroundWorker class takes care of other tricky aspects, such as sending your progress reports to the main GUI thread (where you can safely update the user interface).
Your going to want to create a worker thread that performs the task and occasionally reports its update to the form thread. If you do all of your work in the UI thread, your UI will be locked and won't update the progress/label correctly.
Before you start the worker thread, calculate the total number of steps you believe the process will take. Start the worker thread. After each unit of work, you Invoke an update method on the UI thread to increment the process.
You'll want to look at the BackgroundWorker class.
If your application will have several of these, I recommend creating a process interface (e.g. IProgressProcess). This interface will contain methods for executing a process and reporting updates. You will create all of your process classes by implementing from this interface. Write a control that contains a progress bar and accepts an IProgressProcess through a constructor or property. It can then use your custom process to execute and move along the progress bar. Then you can have your custom progress control send events when the process is complete or canceled.
This usually happens if you try to update the UI on the same thread where the operation is occurring. There are a couple of different ways that you could accomplish this.
You can update the UI with the BeginInvoke method.
You can use a BackgroundWorker component.
The reason that you don't see any change, is that the change causes a message to redraw the label, but the main thread is busy working so it doesn't respond to the message.
The simplest solution would be to just call the Application.DoEvents after updating the label. That works as a quick fix for your immediate problem, but it still will leave the application unresponsive in any other way.
The good solution would to start the operation in a separate thread. That way your main thread is free to handle messages while the operation is running. However working in a separate thread means a litte more work when communicating with the UI. If you want to update controls, you have to use the Invoke method to start a method that runs in the main thread so that it has access to the controls. Alternatively you can just update a variable in the thread, and have a timer control that periodically checks for changes in the variable and updates the label accordingly.

Threading in .net

I have a simple example of a winforms application where i choose a directory in a directory chooser and click a button to loop through the directory and copy each file in the directory into another directory.
i want to do the file copy on a background thread to avoid locking the GUI.
i am looking for the simplest solution to:
Create the background thread
Pass the source and destinations in
Get a callback on progress so i can show a progress bar on the GUI thread
I would recommend using the BackgroundWorker class.
Example.
In addition to the above answer, I'd add that the BackgroundWorker is ideal for this as it can give you progress updates too. Just make sure you prevent reentry - that is you need to prevent the situation where the user could start the background worker again before it has completed.

Categories

Resources