[英]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 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? 我已经看到并使用了Form.Refresh(),Form.Update()和Application.DoEvents()的各种组合,但是想知道最好的方法是什么?
您可以在代码的不同位置使用Application.DoEvents()进行伪造,但实际上应该将工作放到另一个线程中。
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. Bgw使操作变得简单:DoWork事件在另一个线程上运行,但是ProgressChange和Completed在主线程上同步。
Find a few examples and be careful when you use shared data inside DoWork. 查找一些示例,并在DoWork中使用共享数据时要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.