【发布时间】:2011-11-24 03:06:09
【问题描述】:
我有一个将数据抓取到 csv 文件的应用程序,程序每 5 秒抓取一次数据。问题是在后台发生这种情况时我无法执行任何其他操作。我正在使用计时器来完成工作。
我发现了backgroundworker 控件并尝试使用它,并在表单减速方法中设置了RunWorkerAsync(在initializeComponent 之后),但它并没有开始。我在下面给出了 backgroundworker 的编码。
这里需要纠正什么?如果这不是正确的方法,那么在我能够进行其他形式活动的同时,让后台进程发生的最佳方法是什么。谢谢
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//int a = (int)(e.Argument);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
}
else
{
timerQuote.Enabled = true;
timerQuote.Start();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("grabbing is terminated");
}
}
//Form decleration
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
...
}
【问题讨论】:
标签: c# .net winforms multithreading backgroundworker