public partial class MainForm : Form
{
private System.ComponentModel.BackgroundWorker backgroundCalculator; public MainForm()
{
InitializeComponent();
backgroundCalculator = new BackgroundWorker();
backgroundCalculator.WorkerReportsProgress = true;
backgroundCalculator.WorkerSupportsCancellation = true;
backgroundCalculator.DoWork += new DoWorkEventHandler(backgroundCalculator_DoWork);
backgroundCalculator.ProgressChanged += new ProgressChangedEventHandler(backgroundCalculator_ProgressChanged);
backgroundCalculator.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundCalculator_RunWorkerCompleted);
updateStatus(String.Empty);
}
private int getNextPrimeAsync(int start, BackgroundWorker worker, DoWorkEventArgs e)
{
int percentComplete = 0; start++;
while (!isPrime(start))
{
// Check for cancellation
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
start++;
percentComplete++;
worker.ReportProgress(percentComplete % 100);
}
}
return start;
} void backgroundCalculator_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
updateStatus("Cancelled.");
}
else if (e.Error != null)
{
reportError(e.Error);
}
else
{
reportPrime((int)e.Result);
}
calcProgress.Value = 0;
} void backgroundCalculator_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
updateProgress(e.ProgressPercentage);
}
void backgroundCalculator_DoWork(object sender, DoWorkEventArgs e)
{
int start = (int) e.Argument;
e.Result = getNextPrimeAsync(start, (BackgroundWorker)sender, e);
} private void nextPrimeAsyncButton_Click(object sender, EventArgs e)
{
updateStatus("Calculating..."); int start;
Int32.TryParse(textBoxPrime.Text, out start); if (start == 0)
{
reportError("The number must be a valid integer");
}
else
{
// Kick off the background worker process
backgroundCalculator.RunWorkerAsync(int.Parse(textBoxPrime.Text));
}
} private void cancelButton_Click(object sender, EventArgs e)
{
if (backgroundCalculator.IsBusy)
{
updateStatus("Cancelling...");
backgroundCalculator.CancelAsync();
}
} // Update the Status label
private void updateStatus(string status)
{
calcStatus.Text = status;
} // Indicate progress using progress bar
private void updateProgress(int percentComplete)
{
calcProgress.Value = percentComplete;
}
}
{
private System.ComponentModel.BackgroundWorker backgroundCalculator; public MainForm()
{
InitializeComponent();
backgroundCalculator = new BackgroundWorker();
backgroundCalculator.WorkerReportsProgress = true;
backgroundCalculator.WorkerSupportsCancellation = true;
backgroundCalculator.DoWork += new DoWorkEventHandler(backgroundCalculator_DoWork);
backgroundCalculator.ProgressChanged += new ProgressChangedEventHandler(backgroundCalculator_ProgressChanged);
backgroundCalculator.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundCalculator_RunWorkerCompleted);
updateStatus(String.Empty);
}
private int getNextPrimeAsync(int start, BackgroundWorker worker, DoWorkEventArgs e)
{
int percentComplete = 0; start++;
while (!isPrime(start))
{
// Check for cancellation
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
start++;
percentComplete++;
worker.ReportProgress(percentComplete % 100);
}
}
return start;
} void backgroundCalculator_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
updateStatus("Cancelled.");
}
else if (e.Error != null)
{
reportError(e.Error);
}
else
{
reportPrime((int)e.Result);
}
calcProgress.Value = 0;
} void backgroundCalculator_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
updateProgress(e.ProgressPercentage);
}
void backgroundCalculator_DoWork(object sender, DoWorkEventArgs e)
{
int start = (int) e.Argument;
e.Result = getNextPrimeAsync(start, (BackgroundWorker)sender, e);
} private void nextPrimeAsyncButton_Click(object sender, EventArgs e)
{
updateStatus("Calculating..."); int start;
Int32.TryParse(textBoxPrime.Text, out start); if (start == 0)
{
reportError("The number must be a valid integer");
}
else
{
// Kick off the background worker process
backgroundCalculator.RunWorkerAsync(int.Parse(textBoxPrime.Text));
}
} private void cancelButton_Click(object sender, EventArgs e)
{
if (backgroundCalculator.IsBusy)
{
updateStatus("Cancelling...");
backgroundCalculator.CancelAsync();
}
} // Update the Status label
private void updateStatus(string status)
{
calcStatus.Text = status;
} // Indicate progress using progress bar
private void updateProgress(int percentComplete)
{
calcProgress.Value = percentComplete;
}
}
解决方案 »
- C#循环输出问题
- 存储过程怎么用多线程
- 寻求帮助 (IE6输入问题)
- 如何点一下button 数字就加1(二十分)
- 麻烦好心人帮忙解释下下面这段代码~~非常感谢
- 谢谢帮忙
- 为什么 listView1.SelectedItems.Clear(); 不起作用?
- 这样写为什么连为上数据库?
- 为什么生成的解决方案不能拷贝,提示源文件正在使用
- DevExpress 中 XtraTabbedMdiManager 用法
- 程序运行一段时间后出现Only one usage of each socket address (protocol/network address/port) is normally permitted错误
- 如何实现鼠标移动到某个点上时,旁边会自动的显示详细信息
mBackgroundWorker.RunWorkerAsync(arg);
这里有重写,如果不需要传递参数直接mBackgroundWorker.RunWorkerAsync();3. 编辑DoWork事件代码:
e.Argument为mBackgroundWorker.RunWorkerAsync(arg);对应的参数
之所以使用进度条,肯定是有循环的,在循环中报告进度:
worker.ReportProgress(i * 100 / num, obj );
其中第一个参数是当前进度的百分之多少,obj为你要传递的UserState,如果没有可以不要4. 编辑ProgressChanged事件代码:
e.ProgressPercentage为进度的百分数,e.UserState为刚才传递过来的object
在这个事件中可以调用ui的进度条和其他控件:
mToolStripProgressBar.Value = e.ProgressPercentage;5. 编辑RunWorkerCompleted事件代码: