c# opennetCF background worker stops after 100 iterations
I have a background worker that stops after 100 iterations. Like this:
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
bgWorker.DoWork += new OpenNETCF.ComponentModel.DoWorkEventHandler(this.bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new OpenNETCF.ComponentModel.RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted);
bgWorker.ProgressChanged += new OpenNETCF.ComponentModel.ProgressChangedEventHandler(this.bgWorker_ProgressChanged);
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
for(i=0; i<开发者_开发问答;300; i++)
{
bgWorker.ReportProgress(i, i);
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.labelProgress.Text = e.UserState.ToString();
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("finished loading...");
}
What happens is that the labelProgress' value stops at 100, and the messagebox pops up and says "finished loading...".
Anybody have an idea of what is wrong. Why does the thread stop at the 101 iteration?
Thanks in advance.
Your code is crashing the BGW thread. Be sure to display the value of e.Error in the RunWorkerCompleted event handler if it isn't null.
From the MSDN Library article for Backgroundworker.ReportProgress:
percentProgress
Type: System.Int32The percentage, from 0 to 100, of the background operation that is complete.
Fix:
bgWorker.ReportProgress(0, i);
I've tried this (using .Net 4) and it works fine - the label displays 299 when the MessageBox appears. However I'm not using the OpenNETCF
classes - maybe they are having some effect? Do you get the same behaviour if you replace OpenNETCF.ComponentModel
with System.ComponentModel
?
精彩评论