Updating a Progress Form
-
I have a windows forms app which goes and does some processing. While this processing is being done I have created a progress form which I want to show the current progress (this is not working correctly). The code is:
ProgressForm frm = new ProgressForm(); //this is just a simple Form frm.ProgressText = "Do step 1..."; //this sets some text on a label DoStep1(); frm.ProgressText = "Do step 2..."; DoStep2(); frm.ProgressText = "Do step 3..."; DoStep3(); frm.ProgressText = "Completed";
This code will show the progress form, but it will not update the label text until all the steps have completed, then the progress form show "Completed", while the processing is still happening it shows nothing. Anyone know a solution? thanks
-
I have a windows forms app which goes and does some processing. While this processing is being done I have created a progress form which I want to show the current progress (this is not working correctly). The code is:
ProgressForm frm = new ProgressForm(); //this is just a simple Form frm.ProgressText = "Do step 1..."; //this sets some text on a label DoStep1(); frm.ProgressText = "Do step 2..."; DoStep2(); frm.ProgressText = "Do step 3..."; DoStep3(); frm.ProgressText = "Completed";
This code will show the progress form, but it will not update the label text until all the steps have completed, then the progress form show "Completed", while the processing is still happening it shows nothing. Anyone know a solution? thanks
You don't give your label a chance to paint itself because the main (UI-)Thread is busy performing your steps. Either put a call to
Application.DoEvents();
after each text update (beware - that's the quick&dirty way that can lead to other, unexpected problems) or do it correctly: Put the calls toDoStepN()
into a separate thread so your UI will remain responsive. The classBackgroundWorker
makes it quite easy.Regards, mav -- Black holes are the places where God divided by 0...