当前位置:   article > 正文

C# Task.Run Task.WaitAll await async

c# task.run
  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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/104473
推荐阅读
相关标签
  

闽ICP备14008679号