当前位置: 代码迷 >> 综合 >> C# Task.Run Task.WaitAll await async
  详细解决方案

C# Task.Run Task.WaitAll await async

热度:103   发布时间:2023-10-13 12:28:11.0

结论

  1. BeginInvoke相当于UI线程,如果在里面进行大量计算、延时,会卡画面。
  2. async用于修饰方法,await用于修饰是否等待。
        private static string TestString="123";private async void button7_Click(object sender, EventArgs e){
    //async await例子在这里richTextBox1.Text=await Test4();//Thread.Sleep(3000);richTextBox1.Text = richTextBox1.Text +"\n 4444 Hi I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;}private async Task<string> Test4(){
    Task<string> t = Task.Run(() => {
    Thread.Sleep(3000);TestString = "3333 Hi I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;return TestString;});return await t;//Task.WaitAll(t);//richTextBox1.Text = richTextBox1.Text +// "\n 4444 Hi I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;//richTextBox1.Text = richTextBox1.Text + "\n " + await t;}private void button8_Click(object sender, EventArgs e){
    Task t = Task.Run(()=> TestAsync());}private void TestAsync(){
    richTextBox1.BeginInvoke(new Action(() => {
    Thread.Sleep(3000);//此处是UI线程,会卡画面richTextBox1.Text = "Hi I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;}));}private void button9_Click(object sender, EventArgs e){
    //可以实现没有await和async的效果Task t0 = Task.Run(() => T5());}private void T5(){
    Task t1 = Task.Run(() => T6());//另开一个线程不会卡画面Task.WaitAll(t1);richTextBox1.BeginInvoke(new Action(() => {
    richTextBox1.Text = TestString;//只更新画面,不要写大量计算}));}private void T6(){
    Thread.Sleep(3000);TestString= "6666 Hi I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;}
private void button3_Click(object sender, EventArgs e){
    Task t0 = Task.Run(() => T1() );Task t1 = Task.Run(() => T2() );}private void T1(){
    for (int i = 0; i < 1000; i++){
    richTextBox1.Text = richTextBox1.Text + " T1:" + i.ToString();Thread.Sleep(3000);}}private void T2(){
    for (int i = 0; i < 1000; i++){
    richTextBox1.Text = richTextBox1.Text + " T2:" + i.ToString();Thread.Sleep(5000);}}private void T3(){
    int i;for ( i= 0; i < 10; i++){
    richTextBox1.BeginInvoke(new Action(() => {
    richTextBox1.Text = richTextBox1.Text + " T1:" + i.ToString();}));Thread.Sleep(1000);}}private async void button5_Click(object sender, EventArgs e){
    //Console.WriteLine("111 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);richTextBox1.Text = richTextBox1.Text + " 111 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;await Test();//Console.WriteLine("222 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);richTextBox1.Text = richTextBox1.Text + "\n 222 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;}private async Task AsyncMethod(){
    var ResultFromTest = Test();string Result = await ResultFromTest + " + AsyncMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;//Console.WriteLine(Result);//返回值是Task的函数可以不用return}//这个函数就是一个耗时函数,可能是IO操作,也可能是cpu密集型工作。private async Task<string> Test(){
    var task = Task.Run(() => {
    //Console.WriteLine("Helo I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);richTextBox1.Text = richTextBox1.Text + "\n Helo I am Test. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;Thread.Sleep(5000);//Console.WriteLine("Helo I am Test after Sleep(5000). My Thread ID is :" + Thread.CurrentThread.ManagedThreadId);richTextBox1.Text = richTextBox1.Text + "\n Helo I am Test after Sleep(5000). My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;return "Hi I am Test";});return await task;}private async void button6_Click(object sender, EventArgs e){
    richTextBox1.Text = " 111 balabala. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;var t = Task.Run(() => {
    Thread.Sleep(3000);return "Hi I am Test";});richTextBox1.Text = richTextBox1.Text+"\n "+await t;}
  相关解决方案