[英]Background Worker in windows form application C# does not block UI
I am using background worker in my win form application I am showing progress bar form for a long running process and that long running process is on background worker. 我在win窗体应用程序中使用后台工作程序,我正在为长时间运行的进程显示进度条窗体,而该长时间运行的进程正在后台工作程序上。
Note: I have used background worker for showing progress bar in marquee style. 注意:我已经使用背景工作人员以字幕样式显示进度条。
Problem I am facing is due to background worker my User interface gets responsive, but I don't want it to be responsive. 我面临的问题是由于后台工作人员导致我的用户界面反应灵敏,但我不希望它会灵敏。
My code is as below: 我的代码如下:
ProgressBarForm progForm = new ProgressBarForm();
progForm.Show();
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork+= myMethod;
worker.RunWorkerAsync();
Considering that you are using a form to display the progress, you can use ShowDialog
instead of Show
. 考虑到您正在使用表单显示进度,可以使用
ShowDialog
代替Show
。 This will open the window as a modal dialog, blocking UI actions to your underlying window until the progress window is closed. 这将以模式对话框形式打开窗口,阻止UI操作进入您的基础窗口,直到关闭进度窗口。
Some things to consider: 要考虑的一些事情:
ShowDialog
is a blocking call, so call it after starting the background worker. ShowDialog
是一个阻止调用,因此在启动后台工作程序之后调用它。 If you don't want your user interface to be used, set .Enabled = false; 如果您不希望使用用户界面,请设置.Enabled = false;。 on each of the form controls, then reverse the changes in _RunWorkerCompleted.
在每个窗体控件上,然后撤消_RunWorkerCompleted中的更改。
Just remember to leave any cancel / close buttons enabled :) 只要记住让取消/关闭按钮保持启用状态即可:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.