C#后台工作者更新状态标签 [英] C# background worker to update status label
问题描述
这应该是一件相当简单的事情;但是,我一直无法弄清楚.
This should be a fairly simple thing; however, I've been unable to figure this out.
/// This section is located in the InitializeComponent() method
/// form's class, i.e. partial class frmMain { .... }
this.bgw = new System.ComponentModel.BackgroundWorker();
this.bgw.WorkerReportsProgress = true;
this.bgw.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bgw_DoWork);
this.bgw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bgw_ProgressChanged);
/// This code is located in public partial class frmMain : Form { .... }
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100); // Wait 100 milliseconds
//Console.WriteLine(i);
bgw.ReportProgress(i);
}
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Update status label
lblStatus.Text = e.ProgressPercentage.ToString();
}
// New code added to question after edit
public frmMain()
{
InitializeComponent();
bgw.RunWorkerAsync();
// some more stuff...
}
后台工作程序运行正常;但是,它没有正确更新其进度.如果我在DoWork事件中取消注释行,则可以看到状态已正确更新;但是,直到完成主线程中的任务(繁重的数据库计算工作)后,ProgressChanged事件才被触发.
The background worker is running correctly; however, it is not correctly updating its progress. If I uncomment the commented line in the DoWork event, I able able to see the status updated correctly; however, the ProgressChanged event does not get triggered until after the task (heavy database computational stuff) in the main thread is finished.
这使用的是.NET Framework 4,并且是Windows窗体应用程序.
This is using .NET Framework 4 and is a Windows Forms Application.
修改
有关代码的位置,请参见上面代码中的注释.
See the comments in the above code for where the code is located.
更多详细信息
正在执行的代码涉及在数据库上执行多个查询.我没有公开该代码的自由.至于该代码的执行方式,我实际上不知道,因为我是由另一个开发人员交给的.dll,并被告知仅在访问数据库时才使用它....
The code that is being executed involves executing multiple queries on a database. I am not at liberty to disclose that code. As far as how that code is being executed, I actually do not know, as I was handed a .dll by another developer and was told to only use that when accessing the database....
修改
更多内容"部分中的代码如下:
The code in the "some more stuff" section was moved to be as follows
private void frmMain_Load(object sender, EventArgs e)
{
// some more stuff... aka run queries!
}
推荐答案
您的BackgroundWorker
代码就可以了.问题是您在其他地方(在这种情况下,在您的构造函数或FormLoad
中)有阻塞UI线程(通过执行同步数据库请求)的代码.您需要做一些事情以确保此代码在非UI线程中运行.这可能意味着使用现有的BackgroundWorker
来执行其他长时间运行的任务;也可以使用Task.Factory.StartNew
或其他某种线程机制来使其在非UI线程中运行.
Your BackgroundWorker
code is just fine. The problem is that you have code elsewhere (in this case, in your constructor or FormLoad
) that is blocking the UI thread (by performing a synchronous database request). You need to do something to ensure that this code is run in a non-UI thread. This could mean using this existing BackgroundWorker
to perform those other long running tasks; it could also be done by using Task.Factory.StartNew
or some other threading mechanism to have it run in a non-UI thread.
一旦未阻止UI线程,您将在UI中看到您在ProgressChanged事件处理程序中所做的更新.
Once the UI thread is not being blocked you will see the updates made in your `ProgressChanged event handler reflected in the UI.
这篇关于C#后台工作者更新状态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!