一个你能解决这个问题的方法是使用异步和等待,就像这样:
public void Main()
{
AsyncExample();
Console.WriteLine("prints first");
}
public async Task AsyncExample()
{
Task<string> executesSeparately = ExecutesSeparately();
string result = await longRunningTask;
Console.WriteLine(result);
}
public async Task<string> ExecutesSeparately()
{
await Task.Delay(2000);
return "prints second";
}
正如您在输出窗口中看到的,在AsyncExample()完成并写入一行之前,将执行Console.WriteLine(“首先打印”)。
解决这个问题的另一种方法是使用BackgroundWorker。
public string DoSomethingQuick()
{
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
DoSomethingThatTakes10Minutes();
};
backgroundWorker.RunWorkerAsync();
return "Process Started";
}
需要注意的是,在很多情况下,单独的线程将无法改变非静态从主线程创建的对象,所以你要么需要使用调度员或包含UI线程在BackgroundWorker.ReportProgress()调用中的逻辑,它自动在UI线程上工作。这看起来会像这样:使用异步
var backgroundWorker = new BackgroundWorker() { WorkerSupportsCancellation = true };
backgroundWorker.ReportProgress += delegate(object s, ProgressChangedEventArgs e)
{
DoSomethingThatTakes10MinutesAndExecutedOnUiThread();
}
backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs e)
{
backgroundWorker.ReportProgress(0); // the progress value is irrelevant
};
请参阅[这个答案](http://stackoverflow.com/questions/14455293/async-and-await#answer-19985988)和等待。 – 2014-10-28 23:41:31
我可以问你如何在你的搜索中找不到什么异步的东西? – Default 2014-10-28 23:47:07
Downvoting,因为这个问题没有显示研究工作。如果您已经完成了研究,但有些事情不清楚,请将其添加到您的问题中。 – Default 2014-10-28 23:48:07