我有一个简单的应用程序,它触发了一系列数据密集型任务。我对WinForms不是很有经验,我想知道在不锁定界面的情况下最好的方法。可以重复使用backgroundWorker,还是有其他方法可以做到这一点?
由于
答案 0 :(得分:7)
backgroundWorker
可以使用。
它的好处 - 它允许您更新进度条并与UI控件交互。 (WorkerReportsProgress
)
它还有取消机制。 (WorkerSupportsCancellation
)
答案 1 :(得分:5)
BackgroundWorker
是一个包含通知同步的线程。例如,如果您想在扫描完成时更新UI,则常规Thread
无法访问UI对象(只有UI线程可以执行此操作);因此,BackgroundWorker
提供了一个Completed事件处理程序,该处理程序在操作完成时在UI线程上运行。
有关详细信息,请参阅:Walkthrough: Multithreading with the BackgroundWorker Component (MSDN)
和一个简单的示例代码:
var worker = new System.ComponentModel.BackgroundWorker();
worker.DoWork += (sender,e) => Thread.Sleep(60000);
worker.RunWorkerCompleted += (sender,e) => MessageBox.Show("Hello there!");
worker.RunWorkerAsync();
答案 2 :(得分:3)
后台工作人员将是一个很好的选择
有关详情,请点击此处 http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
答案 3 :(得分:3)
您可以将BackgroundWorker用于此类要求。以下是updates a label status based on percentage task [long running] completion
的示例。此外,还有一个示例业务类,它设置了一些值,并通过ProgressChanged
处理程序将值设置回UI。 DoWork
是您编写长期运行任务逻辑的地方。复制 - 在Winforms应用程序&添加标签和后台工作组件后粘贴下面的代码试一试。您可以跨各种处理程序[RunWorkerCompleted, ProgressChanged, DoWork]
进行调试,并查看InitWorker
方法。请注意cancellation feature
。
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
private BackgroundWorker _worker;
BusinessClass _biz = new BusinessClass();
public Form3()
{
InitializeComponent();
InitWorker();
}
private void InitWorker()
{
if (_worker != null)
{
_worker.Dispose();
}
_worker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
_worker.DoWork += DoWork;
_worker.RunWorkerCompleted += RunWorkerCompleted;
_worker.ProgressChanged += ProgressChanged;
_worker.RunWorkerAsync();
}
void DoWork(object sender, DoWorkEventArgs e)
{
int highestPercentageReached = 0;
if (_worker.CancellationPending)
{
e.Cancel = true;
}
else
{
double i = 0.0d;
int junk = 0;
for (i = 0; i <= 199990000; i++)
{
int result = _biz.MyFunction(junk);
junk++;
// Report progress as a percentage of the total task.
var percentComplete = (int)(i / 199990000 * 100);
if (percentComplete > highestPercentageReached)
{
highestPercentageReached = percentComplete;
// note I can pass the business class result also and display the same in the LABEL
_worker.ReportProgress(percentComplete, result);
_worker.CancelAsync();
}
}
}
}
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
// Display some message to the user that task has been
// cancelled
}
else if (e.Error != null)
{
// Do something with the error
}
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage);
}
}
public class BusinessClass
{
public int MyFunction(int input)
{
return input+10;
}
}
}