I have a small application to convert several file formats, with a main windows form which contains several text boxes, buttons, etc. I have another "Document" class which handles all of the actual conversion work, and my form essentially iterates through all of the files, and calls the proper Document methods to convert the files. While this is happening, however, the form stops responding. I created another simple form with a progress bar and a cancel button to spawn when the conversion starts to provide some feedback to our (easily rattled) users. When the new form loads, however, all of the controls are white boxes, and it too stops responding. Once the conversion completes, the progress bar closes properly, and the main form becomes responsive again.
Is there a simple way to make these two forms independent, so that the progress bar can operate even while the other form is unresponsive?
The simplest solution is to have your processing done on a background thread using the BackgroundWorker component. You can drag it in from the VS toolbox. It allows you to update the UI thread with progress notifications so you can update your progress bar to show realistic values (something much more user-friendly than having a "marquee" style progress bar).
You should use two threads so that the form continues to respond while you do work.
This is so common that .NET has a BackgroundWorker control that wraps some of this up for you.
Just call Application.DoEvent() once in a while, probably in your loop.
Not as right as BackgroundWorker, but it's even simpler.
The simplest solution is to have your processing done on a background thread using the BackgroundWorker component. You can drag it in from the VS toolbox. It allows you to update the UI thread with progress notifications so you can update your progress bar to show realistic values (something much more user-friendly than having a "marquee" style progress bar).
Related
Im creating a c# windows form and ive come accross a problem.
When the window opens I am wanting to run a bit of code which can make the front console appear to freeze. The code runs fine but I want to show the status of the program in the status strip at the bottom of the page. I am running the code in the action Form.Shown. However the code does not update the status bar until everything is shown. I can change the label no problem.
How would I go about loading the window and then running the code and updating the status bar (like a background task)?
What areas would I need to look at to get this information?
You could use a BackgroundWorker to do this
Set the WorkerReportsProgress property to true and in the DoWork eventhandler raise the ReportProgress event
in the eventhandler for the ProgressChanged place your logic to update your progresbar
there is a example on msdn
You can use Multithreading to avoid freezing the forms. It means, you separate your form and the code (that you want to run in parallel) in different threads so that the form doesn't wait for the code completes. You can monitor the progress of the code via events.
I need to have a progress bar in a popup window, while my main thread is doing some heavy calculations.
I'm using an Oracle API , all the heavy work is done calling 1 function of this API, so I can't use the backgroudworker update becasue I don't have the API's code.
I tried to create a new form with an ProgressBar and a Timer, but the timer_Click event is never fired!!(???)
Any Idea??
while my main thread is doing some heavy calculations.
Wrong. Never do any heavy calculations inside your main thread in a Windows application or your users will simply hate you.
If your API doesn't provide a way to report the progress then the best you could do is show some rotating animation while the calculation is in progress because you will not be able to know the exact percentage of work done. So put the calculation inside a BackgroundWorker, show some spinning animation before running the calculation (which should itself be done inside the DoWork event) and hide the animation once the work is done (RunWorkerCompletedEvent).
The ProgressBar has a Style property that you could set to Marquee to emulate an infinite progression.
I have a MDI form in which, once I click on any menu item, the associated form is shown. Sometimes, forms take time to load, so I would like to show a progress bar for long-running calls.
Since few forms take time to open, you should be moving the form loading code into a separate thread using background worker(This will avoid UI from freezing). You can have WorkerReportsProgress property set to true, so that you can use it to show the progress in the progress bar in ProgressChanged event handler, while the loading.
If you do not have much idea about how to work with Background worker, I recommend you learn how to use them --- See this
Although this is a VB.NET example, the essential part is to get the idea, so I would recommend this excellent article that answers your question:
http://www.dreamincode.net/forums/topic/58239-progress-bar-while-loading-a-child-form/
PS: It would not hurt to google it first.
I have a WinForms application, and wanted to add some nice WPF controls, one of them being an 'indeterminate' progress bar.. which just animates nicely until I tell it to stop.
I have done this, and was racking my brains as to why it wasn't animating (changing the value of the progress bar).
I eventually tried showing my form (containing the ElementHost) modally, and hey presto, it animates, but I want to show the form non-modally, as I want to continue processing behind the scenes.
Is there some kind of setting that tells the ElementHost window to continue 'animating'?
Thanks
Rich.
There are many ways to do this , the simplest one is to use a backgroundworker for the lengthy task. The Backgroundworker has an event to report progress. Handle this event and in the handler change your progressbar's value. Just having an animation in the main thread while still doing work on the main thread will not work right ...
One other aproach you may try (though is not what I would recommend for a healty app) is to implement a DoEvents function and call it in the main thread when you want the progress bar to get updated ... Here is the link for the DoEvents implementation:
MSDN DoEvents sugestion
I would Strongly recommend the first approach though
As I mention in my comment above, the solution is to run the processing in a separte thread, which allows the .net Main GUI thread do its stuff, and animate the progress bar.
When I click on anywhere on my application when the application is busy it changes the cursor into a generic wait cursor. Is there anyway I can code it for an animated cursor?
This is because you must be doing some heavy operation on main UI thread. Do your processing in background (in separate thread).
You may use BackgroundWorker or Thread class to achieve this.
Windows will always use the generic "busy" cursor if the user tries to interact with it and your application is not responding.
The solution to this is to not do processing on the UI thread - do it on other threads, so your UI remains responsive.
If your UI is still responding, you can set a custom cursor to indicate that your application is busy processing.
if you are using the windows application in ASP.NET then there is a option realted to cursor in properties of the form(nit clearlly remember, it was like wait.cursor). You can use that to display the custom cursor at the time of processing also.