C ා use await to call button event handler asynchronously

Click the button to implement asynchronously to realize the non blocking state of the interface.
The asynchronous development mode recommended in C ා uses async to prompt for possible asynchronous operations, and await keyword to wait for tasks to wait for asynchronous operations.
For the methods with Async suffix provided in. net library, await method can be used to wait for execution asynchronously, which is convenient for the development process.
If you want to make your original synchronous method program asynchronous method, you can refer to the paint method of the following example.
The paint method is responsible for drawing the border color of the text box as blue.
The paintAsync() method wraps the paint and adapts it to tasks that can be accessed asynchronously.
In the button1 click method, execute await paintAsync() to call the button event asynchronously

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "__1\r\n");
                //await Task.Run(new Action(paint));
                await paintAsync();    //Methods after await must be of type task
                IOTest();  //The await StreamWrite.WirteAsync method implements asynchronous writes.
                await NoneTest();   //Use new Task to create tasks and asynchronous Start to Start, which is equivalent to Task.Run
                textBox1.Text += "await end";
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "__2\r\n");

            }
            finally
            {

            }
        }

        public Task paintAsync()
        {

            return Task.Run(new Action(paint));
        }

        public async void IOTest()
        {
            StreamWriter sw = new StreamWriter("./streamIO.txt");

            await sw.WriteAsync("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + Thread.CurrentThread.ManagedThreadId.ToString());   //The thread number here is the main interface thread
            sw.Flush();
            sw.Close();
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "__4\r\n");

        }
        public Task NoneTest()
        {
            Task t1 = new Task(() => { Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "__5\r\n"); });   //Thread number is background thread number
            t1.Start();
            return t1;
        }

        public void  paint()
        {
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "__3\r\n") ; //Thread number is background thread number
            using (Graphics g = this.CreateGraphics())
            {
                Rectangle rect = new Rectangle(textBox1.Location.X - 1, textBox1.Location.Y - 1, textBox1.Width, textBox1.Height);
                using (Pen pen = new Pen(Color.Blue))
                {
                    g.DrawRectangle(pen, rect);
                }
                rect.Inflate(-1, -1);
                g.DrawRectangle(new Pen(Color.Pink), rect);
            }

        }
    }
}

Keywords: ASP.NET Windows

Added by shooff2332 on Tue, 12 May 2020 17:36:54 +0300