可以先看之前关于task、Thread、backgroudwork的文章。

从性能和使用来说,Thread、threadPool、backgroudwork都弱于Task。使用task基本上都伴随着async/await,但并不是async/await都必须有task。async/await需要解决的问题就是,遇到耗时的任务,后台会进行等待,不卡界面操作,不卡界面是最主要的,等后台处理完成后,结果就会在界面上出来。本文接着task的例子进行。

之前界面业务回顾:

界面业务说明,label1是加法计算,分别有3个功能,对label1停止加法,暂停加法,继续加法。100 是减法计算

1.先看界面,说一下,label3是B线程开始标记, label4是B线程结束标记。之前的线程停止,暂停,继续依然能使用。

2.load中直接调用

3.AA方法,A方法不变和之前一样

      public async void AA()
        {
            await Task.Run(() =>
            {
                A();
            });
        }

       public void A()
        {
            for (int i = 0; i < 100; i++)
            {
                if (token.IsCancellationRequested)//这个是加法停止的判断
                {
                    return;
                }
                resetEvent.WaitOne();//相当于把resetEvent和线程task1进行绑定
                Thread.Sleep(1000);
                Invoke(new Action(() => label1.Text = i.ToString()));
            }

        }

 4.BB方法,B方法不变,和之前一样

    public async void BB()
        {
            label3.Text = "B线程开始";
            await Task.Run(() =>
            {
                B();
            });
            label4.Text = "B线程结束";
        }
   public void B()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1000);
                Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
            }

        }

 5.效果

 代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        CancellationToken token;
        ManualResetEvent resetEvent = new ManualResetEvent(true);


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AA();
            BB();
        }

        public async void AA()
        {
            await Task.Run(() =>
            {
                A();
            });
        }

        public async void BB()
        {
            label3.Text = "B线程开始";
            await Task.Run(() =>
            {
                B();
            });
            label4.Text = "B线程结束";
        }

        public void A()
        {
            for (int i = 0; i < 100; i++)
            {
                if (token.IsCancellationRequested)//这个是加法停止的判断
                {
                    return;
                }
                resetEvent.WaitOne();//相当于把resetEvent和线程task1进行绑定
                Thread.Sleep(1000);
                Invoke(new Action(() => label1.Text = i.ToString()));
            }

        }

        public void B()
        {
            for (int i = 0; i < 20; i++)
            {
                Thread.Sleep(1000);
                Invoke(new Action(() => label2.Text = (Convert.ToInt16(label2.Text) - 1).ToString()));
            }

        }

        /// <summary>
        /// 停止线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var tokenSource = new CancellationTokenSource();
            token = tokenSource.Token;
            tokenSource.Cancel();
        }
        /// <summary>
        /// 继续
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            resetEvent.Set();
        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            resetEvent.Reset();
        }
    }
}

 拓展

        //无返回值
        public async void A()
        {
            var v = await File.ReadAllBytesAsync("1.txt");
        }
        //有返回值
        public async Task<int> A1()
        {
            var v = await File.ReadAllBytesAsync("1.txt");
            return v.Length;
        }
        //事件上使用
        private async void B_Click(object sender, RoutedEventArgs e)
        {
            await Task.Run(() =>
           {
               File.ReadAllText("1.txt");
           });
        }
        //main上必须加Task
        static async Task Main(string[] args)
        {
            var v = await File.ReadAllBytesAsync("1.txt");
        }
        //返回IAsyncResult
        public static async Task<IAsyncResult> A2()
        {
            var v = await File.ReadAllTextAsync("1.txt");
            return Task.FromResult(v);
        }

Task的方法 

            Task task1 = Task.Run(() =>
            {
                A();
            });
            Task task2 = Task.Run(() =>
            {
                B();
            });
            Task.WhenAll(task1, task2);   //全部执行完成
            Task.WaitAny(task1, task2);   //其中一个执行完成

按钮异步 

        private async void button1_Click(object sender, EventArgs e)
        {
            var t = Task.Run(() => 
            {
                Thread.Sleep(4000);
                return "123";
            }
            );
            label1.Text = await t;
        }

必须带async,必须带await

必须带async,必须带await

必须带async,必须带await

await后面必须直接带异步的方法,例如ReadAllTextAsync;或者间接的带异步方法,也就是匿名方法的表示,例如Task.Run(()=>{})。