C# 窗口在繁忙时变为空白 ->进度条?但如何呢?
我有一个程序可以执行一些复制作业(通过 File.Copy),可能会持续几分钟。当用户此时将另一个窗口带到前台时,程序窗口会变为空白,因为它不会重新绘制自身。
现在我想在屏幕中央的额外窗口中显示进度栏,但该窗口也是空白的。
所以我在另一个线程中启动它,但没有帮助。
我很确定以前有人这样做过,但我找不到有效的例子。有什么想法吗?
I have a program which does some copy jobs (via File.Copy) that could last a few minutes. When the user brings another window to foreground in this time the program window gets blank since it doesn't redraw itself.
Now I want to show a ProgressBar in an extra window in the center of the screen, but that window get's blank too.
So I startet it in another thread which didn't help.
I'm quite sure someone did this before but I cannot find a working example. Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BackgroundWorker 类 的文档中有一个很好的示例。
There is nice example in the documentation on BackgroundWorker class.
克里斯F是正确的。您的长时间操作应在 BackgroundWorker 线程中完成。您可以使用BackgroundWorker 报告进度,并将其连接到表单上的进度栏。
ChrisF is correct. Your long operation should be done in a BackgroundWorker thread. You can use BackgroundWorker to report progress, and hook that up to a progress bar on your form.
您需要使用
BackgroundWorker
。还有其他方法可以线程化操作,但这一种可能是最简单的,并且将继续在前台执行消息泵送,因此 Windows 不会认为您的应用程序已停止响应。另一种选择是使用
Thread
,并使用< code>Thread.Join 等待后台任务完成,因为Thread.Join
在等待时还会向 Windows 发送标准消息泵信息。You need to thread your operation using a
BackgroundWorker
. There are other ways to thread the operation, but this one is probably the simplest, and will continue to perform message pumping in the foreground so Windows doesn't think your application has stopped responding.Another option would be to use a
Thread
, and useThread.Join
to wait for the background task to complete, sinceThread.Join
also sends standard message pump information to Windows while it waits.您可以使用BackgroundWorker 类。
请参阅此答案。
You can use BackgroundWorker class.
See this answer.