.net 4.5中的async await
.net 4.5中新增C#关键字async await长时间操作任务的福音。
private async void Button_Click_1(object sender, RoutedEventArgs e) { var str= await this.WaitAsynchronouslyAsync(); MessageBox.Show(str); } public Task<string> WaitAsynchronouslyAsync() { TaskCompletionSource<string> tcs = new TaskCompletionSource<string>(); Task.Factory.StartNew(() => { Thread.Sleep(5000); tcs.SetResult("hello"); }); return tcs.Task; }