2022年5月6日 星期五

c# async await非同步

在c#有async await非同步的東西非常好用,不過有些地方需要注意,我這邊舉四個案例

案例一:

寫了一個非同步的method,但呼叫的時候沒有加await

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                TestAsync();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }


        private static async Task TestAsync()
        {
            await Task.Delay(2000);
            Console.WriteLine("Test...");
            throw new Exception("Test Exception");
        }
    }
}


案例一的結果TestAsync等於射後不理,而且method裡面噴錯也不會被catch住喔!!


案例二:

寫了一個void method但有掛async

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TestAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }


        private static async void TestAsync()
        {
            await Task.Delay(2000);
            Console.WriteLine("Test...");
            throw new Exception("Test Exception");
        }
    }
}












案例二的結果會先執行Hello World,所以並不會等待TestAsync的回應,不過是可以catch到exception


案例三:

寫了一個非同步的method,但呼叫的時候有加await

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                await TestAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }


        private static async Task TestAsync()
        {
            await Task.Delay(2000);
            Console.WriteLine("Test...");
            throw new Exception("Test Exception");
        }
    }
}



案例三的結果會等待TestAsync的回應,也可以catch到exception


案例四:

寫了一個非同步的method,先指定task1 task2 最後兩個一起await

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                var task1 = TestAsync();
                var task2 = TestAsync();

                await task1;
                await task2;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }


        private static async Task TestAsync()
        {
            await Task.Delay(2000);
            Console.WriteLine("Test...");
        }
    }
}


案例四的結果會等待task1 task2的回應,也就是說task1與task2會並行執行,也可以catch到exception

沒有留言:

張貼留言