简体   繁体   English

当我的程序使用后台工作程序进行工作时,如何防止表单为空?

[英]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.

相关问题 如何防止我的玩家越过对撞机? - How do I keep my player from going over the collider? 在进行后台处理时,如何在列表框中添加值? - How do i add values to my list box while my background proccessing is going on? 我正在制作一个太空射击游戏,但我的子弹继续向上而不是向外发射并且没有从 Unity 层次结构中删除 - I am making a space shooter but my bullets keep on going up instead of out and not deleting from the Unity Hierarchy 如何在循环中保持C#Windows窗体响应? - How do I keep my C# Windows Form Responsive while it churns loops? 如果表单上有任何未保存的更改,如何防止用户退出程序? - How do I keep the user from exiting the program if there are any unsaved changes on the form? 如何防止我的C#程序在安装时运行? - How do I keep my C# program from running as it's installed? 我如何告诉我的程序它应该继续从数字中减去? - How do I tell the my Program that it should keep subtracting from a number? 如何在后台工作人员中显示其他内容时检查登录表单的用户名和密码 - How Can I Check The Username And Password For Login Form While Showing Sth Else In Background Worker 如何为后台工作人员传递功能? - How do I pass a function for background worker? 如何在后台工作程序中使用匿名管道减少程序的CPU使用率? - How can I reduce the CPU usage of a program using Anonymous Pipes in a background worker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM