这里我使用多个backgroundworker
s 来做一个网络相关的操作。基本上它是一个检查用户是否在我的页面上注册的过程。我有近 1000 个帐户要检查,因此我datagridview
用来导入用户并从中获取用户名以进行检查。我的代码工作正常并显示结果,但问题是在更新状态时datagridview
效果不是很好。在那个长过程方法中,我曾经为方法的每个过程设置状态文本,例如login process started
logged in
failed to login
。但Backgroundworker
不更新状态列。它仅显示所有backgroundworker
完成后的状态。谁能告诉我如何更新每个帐户的状态?提前致谢
这是我的代码:
int threadNum;
public BackgroundWorker bcheker;
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (wbcheckStatus == WorkerStatus.NotStarted || wblogcheckStatus == WorkerStatus.Completed)
{
threadNum = -1;
SetControlsStatus(ProgramStatus.BChecking);
toolStripButton2.Image = aTumblr.Properties.Resources.control_stop;
for (int i = 0; i < 4; i++)
{
bcheker = new BackgroundWorker();
bcheker.DoWork += new DoWorkEventHandler(bcheker_dowork);
bcheker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bchecker_completed);
bcheker.ProgressChanged +=bcheker_ProgressChanged;
bcheker.WorkerReportsProgress = true;
bcheker.WorkerSupportsCancellation = true;
bcheker.RunWorkerAsync();
}
}
else
{
threadNum = 10000;
bcheker.CancelAsync();
SetControlsStatus(ProgramStatus.BlogChecking);
}
}
public void bcheker_dowork(object sender, DoWorkEventArgs e)
{
while (!bcheker.CancellationPending)
{
int rownum = Interlocked.Increment(ref threadNum);
if (rownum >= bchecktableModel.Rows.Count)
{
break;
}
Thread.Sleep(1000);
BlogChecker bc = new BlogChecker(bchecktableModel[rownum, 1].Text, bchecktableModel[rownum, 2]);
bc.check();
wblogcheckStatus = WorkerStatus.Running;
}
if (bcheker.CancellationPending)
{
wblogcheckStatus = WorkerStatus.Completed;
SetControlsStatus(ProgramStatus.BCheckingDone);
}
}
public void bcheker_ProgressChanged(Object sender, ProgressChangedEventArgs e)
{
}
public void bchecker_completed(object sender, EventArgs e)
{
if (threadNum == bchecktableModel.Rows.Count+1)
{
SetControlsStatus(ProgramStatus.BCheckingDone);
wblogcheckStatus = WorkerStatus.Completed;
}
}