.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;
}

 

 

 

posted on 2013-05-24 13:24  beastplus  阅读(449)  评论(0编辑  收藏  举报

导航