C#多线程之Await&Async学习笔记

2022/2/27 20:23:05

本文主要是介绍C#多线程之Await&Async学习笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C# 5.0语法糖,依托Task存在,本身不会产生线程,达到了通过异步方法实现同步效果的目的(解决异步多线程执行顺序难控制的问题)

1.定义

async可以随便添加,可以不用添加await

await只能出在Task前面,但是方法必须声明async,不能单独出现

await/async之后,原本没有返回值,可以返回Task

        原本返回T类型的,返回Task<T> 

        尽量不要返回void,因为不能再await了   

2.执行顺序

namespace AwaitAsyncTest
{
    class Program
    {
        static void Main(string[] args)
        {
            AwaitAsyncClassNew awaitTest = new AwaitAsyncClassNew();
            awaitTest.Show();
            Console.ReadKey();
        }


    }

   class AwaitAsyncClassNew
    {
        public void Show()
        {
            Console.WriteLine($"This is MainThread Start{Thread.CurrentThread.ManagedThreadId}");
            this.NoReturnTask();
            Console.WriteLine($"This is MainThread End {Thread.CurrentThread.ManagedThreadId}");
        }

        public async Task NoReturnTask()
        {
            Console.WriteLine($"This is NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
            Task task = Task.Run(() =>
            {
                Console.WriteLine($"NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Console.WriteLine($"NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");
            });//调用线程发起,启动新线程执行内部动作
            await task;//调用线程回去忙自己的事
            Console.WriteLine($"This is NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");//由子线程完成(如果没有await,应该由调用线程执行)
        }
  }

执行结果:

This is MainThread Start1
This is NoReturnTask Start1
This is MainThread End 1
NoReturnTask Start3
NoReturnTask End3
This is NoReturnTask End3 --本来属于主线程的却被子线程执行了

相当于await的后续代码包装成了一个回调,运行次此回调的线程有多种可能性

将await开始的代码换成

        public async Task NoReturnTask()
        {
            Console.WriteLine($"This is NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
            Task task = Task.Run(() =>
            {
                Console.WriteLine($"NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Console.WriteLine($"NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");
            });
            //await task;
            //Console.WriteLine($"This is NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");
            task.ContinueWith(t => 
                Console.WriteLine($"This is NoReturnTask End{Thread.CurrentThread.ManagedThreadId}"));
        }

结果为

This is MainThread Start1
This is NoReturnTask Start1
This is MainThread End 1
NoReturnTask Start3
NoReturnTask End3
This is NoReturnTask End4

如果再更多层的await,执行顺序依次向后(更像是顺序执行了),相当于用同步的形式去写异步

public async Task NoReturnTask()
        {
            Console.WriteLine($"This is NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
            Task task = Task.Run(() =>
            {
                Console.WriteLine($"NoReturnTask Start{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Console.WriteLine($"NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");
            });
            await task;
            Console.WriteLine($"This is NoReturnTask End{Thread.CurrentThread.ManagedThreadId}");
            //task.ContinueWith(t => 
            //    Console.WriteLine($"This is NoReturnTask End{Thread.CurrentThread.ManagedThreadId}"));
            await Task.Run(() =>
            {
                Console.WriteLine($"NoReturnTask2 Start{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Console.WriteLine($"NoReturnTask2 End{Thread.CurrentThread.ManagedThreadId}");
            });
            Console.WriteLine($"This is NoReturnTask2 End{Thread.CurrentThread.ManagedThreadId}");

        }

结果为

This is MainThread Start1
This is NoReturnTask Start1
NoReturnTask Start3
This is MainThread End 1      --主线程结束
NoReturnTask End3
This is NoReturnTask End3    --执行完task1
NoReturnTask2 Start4     ---执行Task2
NoReturnTask2 End4
This is NoReturnTask2 End4

3.线程ID问题 

4.返回值问题



这篇关于C#多线程之Await&Async学习笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程