tags:

views:

724

answers:

4

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?

+5  A: 

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).

Aaron Lerch
A: 

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.

Giovanni Galbo
A: 

Just call Application.DoEvent() once in a while, probably in your loop. Not as right as BackgroundWorker, but it's even simpler.

David Vidmar
A: 

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).