如果没有更多上下文,就不可能提出非常具体的改进建议。就是说,您当然可以摆脱while 循环。只需使用:
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += delegate
{
//code
};
backgroundWorker.RunWorkerAsync();
请注意,如果您有想要在 BackgroundWorker 任务完成时执行的代码(可能解释了为什么您首先使用 while 循环),这样的事情将起作用(而不是使用 @ 987654325@你之前的循环):
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += delegate
{
//code
};
backgroundWorker.RunWorkerCompleted += (sender, e) =>
{
// code to execute when background task is done
};
backgroundWorker.RunWorkerAsync();
在现代 C# 中,BackgroundWorker 几乎已过时。它现在提供的主要好处是方便的进度报告,实际上可以通过使用Progress<T> 轻松获得。
如果您不需要报告进度,将代码分解为使用async/await 很容易:
await Task.Run(() =>
{
//code
});
// code to execute when background task is done
如果您确实需要报告进度,只是稍微困难一点:
Progress<int> progress = new Progress<int>();
progress.ProgressChanged += (sender, progressValue) =>
{
// do something with "progressValue" here
};
await Task.Run(() =>
{
//code
// When reporting progress ("progressValue" is some hypothetical
// variable containing the progress value to report...Progress<T>
// is generic so you can customize to do whatever you want)
progress.Report(progressValue);
});
// code to execute when background task is done
最后,在某些情况下,您可能需要任务返回一个值。在那种情况下,它看起来更像这样:
var result = await Task.Run(() =>
{
//code
// "someValue" would be a variable or expression having the value you
// want to return. It can be of any type.
return someValue;
});
// At this point in execution "result" now has the value returned by the
// background task. Note that in the above example, the method itself
// is anonymous and so you could just set a local variable at the end of the
// task; the value-returning syntax is more useful when you are calling an
// actual method that itself returns a value, and is especially useful when
// you are calling an `async` method that returns a value (i.e. you're not
// even using `Task.Run()` in the `await` statement.
// code to execute when background task is done
您可以根据需要混合搭配上述技术。