How do I keep a form from going blank while my program working with out using a background worker?
On a regular basis I find myself writing little utility programs that use some loop which takes a while to process. Yet while the loop is going the form no longer refreshes so if you were to move the form, or move another window over it and off, the form would be blank until the loop finishes.
Now I know the correct way to deal with this is to use a background working to do the time consuming task on a separate thread. But I haven't quite got my head around the multi threaded stuff just yet.
So If I'm only going to use one thread what is the best thing to do on each loop to 开发者_JS百科keep the contents of the form up to date. For example there may be a progress bar in the form.
I've seen and used various combonations of Form.Refresh(), Form.Update(), and Application.DoEvents() but was wondering what is the best way to deal with this?
You can fake it with Application.DoEvents() at different locations in the code, but you really should put the work in another thread.
Take it as an opportunity to get into threading, you will have to sooner or later.
The Bgw makes it easy: The DoWork event runs on another thread but ProgressChange and Completed are synchronized on the main thread.
Find a few examples and be careful when you use shared data inside DoWork.
精彩评论