开发者

Updating windows forms panel with progress message during execution

I am trying to show the progress of my windows form application in a panel within the form. However, all the messages show together at t开发者_运维问答he end of when the application completes executing. Is there a way to display the messages as the code - execution "progress" through these messages - just the way it would in an interpreted language?

PS: I am adding the message as a label control to the panel at different points within the code.

Thank you.


You have to invalidate the label every time you change the message:

label1.Text = "Initializing...";
label1.Refresh();

// Do Stuff

label1.Text = "Working...";
label1.Refresh();

// Do Stuff

label1.Text = "Completed.";
label1.Refresh();


Or, it may be worth using a backgroundWorker (or another thread). Whats happening now is that the main thread (the one that also draws the UI) is too busy doing the processing to update the UI.

I'm not sure what language you're using, but Ill assume C#. In that case, create a BackgroundWorker that reports progress. Call the Background worker asynchronously so that it does the processing and use the Reports_Progress event to set the label. You cant set the label in the main Backgroundworker do_work procedure since the label was created by another thread. See this example, it may help (admittedly he sets a progress-bar value but you can just as easily set label text - http://www.dotnetperls.com/progressbar)

If you dont have the Backgroundworker class, you can implement the same logic, just using a different thread.

If you need some more info, let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜