C# async await 异步例子
2021-02-07 07:15
标签:ret ttext 返回值 new patch wait extc text btn C# async await 异步例子 标签:ret ttext 返回值 new patch wait extc text btn 原文地址:https://www.cnblogs.com/zdc-shine/p/11392845.html
private void BtnasyncAwait_Click(object sender, RoutedEventArgs e)
{
SetTextShow("111 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);
AsyncMethod();
SetTextShow("222 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);
}
{
txtInfo.Dispatcher.Invoke(new Action(()=> { txtInfo.Text += DateTime.Now.ToString() + " " + textContent + Linefeed; }));
}
{
txtInfo.Dispatcher.Invoke(new Action(() => { txtInfo.Text = string.Empty; }));
}
{
var ResultFromTimeConsumingMethod = TimeConsumingMethod();
string Result = await ResultFromTimeConsumingMethod + " + AsyncMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;
SetTextShow(Result);
//返回值是Task的函数可以不用return
}
private Task
{
var task = Task.Run(() => {
SetTextShow("Helo I am TimeConsumingMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
SetTextShow("Helo I am TimeConsumingMethod after Sleep(5000). My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);
return "Hello I am TimeConsumingMethod";
});
}