Hi Chris
I apologize if this answer appears twice, but I got an error trying to post
the previous answer, so here goes.
When you do heavy processing inside the GUI thread (main thread in a windows
application) you won't have any processing time to perform GUI stuff. This
will cause a gray and/or non responsive interface.
You should move any heavy processing to a separate thread. The easiest way
to do this is by using a BackgroundWorker object like Gilles points out.
Below is an example of how this can be done using a background worker that
reports progress as well as supports aborting. Add a progressbar control as
well as two buttons with their Click event attached to button1_Click and
button2_Click
private BackgroundWorker worker;
public Form2()
{
InitializeComponent();
worker = new BackgroundWorker();
// Needed to fire the progresschanged event
worker.WorkerReportsProgress = true;
// Needed to be able to stop the processing
worker.WorkerSupportsCancellation = true;
// Any heavy processing should spawn from this event
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
// This event fires when you want it to
worker.ProgressChanged += new
ProgressChangedEventHandler(worker_ProgressChanged );
// This event fires when the worker is done processing
worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);
}
private void button1_Click_1(object sender, EventArgs e)
{
// Resett progressbar
progressBar1.Value = 0;
// Notify the worker object to start working
worker.RunWorkerAsync(@"C:\temp");
// You can put anything as an argument to this method
}
private void button2_Click(object sender, EventArgs e)
{
// Stop working
if (worker.IsBusy)
worker.CancelAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
DateTime start = DateTime.Now;
// This method is called when you use RunWorkerAsync
string path = e.Argument.ToString();
// e.Argument can be anything, including a complex object
for (int i = 0; i < 100; i++)
{
// Did someone cancel the work?
if (worker.CancellationPending)
{
// if so, then simply return
return;
}
// Simulate processing of a single file
// which in this case would take 1 seconds
// to process.
System.Threading.Thread.Sleep(1000);
// Report the number of files processed
worker.ReportProgress(i + 1);
// The parameter says intPercentage,
// but it can be any number you like
}
// the Result property can be used to provide information
e.Result = "100 files processed in " + (DateTime.Now -
start).TotalSeconds + " minutes";
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// If the worker calls ReportProgress this method will be called.
progressBar1.Value = e.ProgressPercentage;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// This method will be called when the worker is done processing or
cancelled
if (e.Result == null)
MessageBox.Show("Processing aborted");
else
MessageBox.Show(e.Result.ToString());
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"ha******@hotmail.com" wrote:
Hi,
I'm building a forms app that parses thousands of text files. My boss
insisted it have a GUI, to which I added an information console and a
progress bar tracking the number of files parsed.
Everything is working fine, except that when the application is run,
it devotes itself entirely to the parsing, and the form with the
console and progress bar just blanks out until the application
finishes running.
I've done mostly web development, so I'm kinda flummoxed about this.
How do I rein my app in a little so that the form is continually
displaying useful information?
Regards,
Chris