简体   繁体   English

C#窗口在繁忙时变为空白->进度栏? 但是如何?

[英]C# Window gets blank when busy -> Progressbar? But how?

I have a program which does some copy jobs (via File.Copy) that could last a few minutes. 我有一个程序执行一些复制作业(通过File.Copy),该作业可能持续几分钟。 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. 现在,我想在屏幕中心的另一个窗口中显示一个ProgressBar,但是该窗口也为空白。

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? 有任何想法吗?

BackgroundWorker类的文档中有一个很好的示例。

You can use BackgroundWorker class. 您可以使用BackgroundWorker类。

See this answer. 看到这个答案。

ChrisF is correct. ChrisF是正确的。 Your long operation should be done in a BackgroundWorker thread. 您的长操作应在BackgroundWorker线程中完成。 You can use BackgroundWorker to report progress, and hook that up to a progress bar on your form. 您可以使用BackgroundWorker报告进度,并将其连接到表单上的进度栏。

You need to thread your operation using a BackgroundWorker . 您需要使用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. 还有其他方法可以对该操作进行线程化,但是这种方法可能是最简单的,并且将继续在前台执行消息泵送,因此Windows不会认为您的应用程序已停止响应。

Another option would be to use a Thread , and use Thread.Join to wait for the background task to complete, since Thread.Join also sends standard message pump information to Windows while it waits. 另一个选择是使用Thread ,并使用Thread.Join等待后台任务完成,因为Thread.Join在等待时还会向Windows发送标准消息泵信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM