Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Task.Run và async Task trong C#

11:12 04-08-2021 10.594 lượt xem 4 bình luận 20:33 04-08-2021

Ví dụ code của Task.Run:

internal class Program
    {
        private static readonly Stopwatch watch = new Stopwatch();
        private static void Main(string[] args)
        {
            watch.Start();
            TaskTest();
            Console.Read();
        }
        /*task run*/
        private static void TaskTest()
        {
            Task task1 = Task.Run(() => Task1());
            Task task2 = Task.Run(() => Task2());
            Task.WaitAll(task1, task2);
            Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
        }
        private static void Task1()
        {
            Task.Delay(5000).Wait();
            Console.WriteLine($"TaskOne\tExecution Time: {watch.ElapsedMilliseconds} ms");
        }
        private static void Task2()
        {
            Task.Delay(2000).Wait();
            Console.WriteLine($"TaskTwo\tExecution Time: {watch.ElapsedMilliseconds} ms");
        }
    }

Ví dụ code của async Task:

internal class Program
    {
        private static readonly Stopwatch watch = new Stopwatch();
        private static void Main(string[] args)
        {
            watch.Start();
            TaskAsyncTest();
            Console.Read();
        }
        /*task async*/
        private static void TaskAsyncTest()
        {
            Task task1 = Task1Async();
            Task task2 = Task2Async();
            Task.WaitAll(task1, task2);
            Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
        }
        private static async Task Task1Async()
        {
            await Task.Delay(5000);
            Console.WriteLine($"TaskOne\tExecution Time: {watch.ElapsedMilliseconds} ms");
        }
        private static async Task Task2Async()
        {
            await Task.Delay(2000);
            Console.WriteLine($"TaskTwo\tExecution Time: {watch.ElapsedMilliseconds} ms");
        }
    }

Kết quả so sánh:

Mọi người cho mình hỏi trong C# giữa Task.Runasync Task khác nhau như thế nào và khi nào dùng Task.Run, khi nào dùng async Task?

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
tqk2811 đã bình luận 20:25 04-08-2021

Đi sâu vào thì cũng khá khó nói và phức tạp, nên search tiếng anh nếu muốn hiểu rõ.

Nói đơn giản thì như nhau trong trường hợp này. 

Với async thì task sẽ chạy cùng 1 thread với thằng gọi nếu có SynchronizationContext trong thread (Thread UI sẽ có cái này - Winform và Wpf). Lưu ý, trong trường hợp này nếu dùng Task.Result hay Task.Wait một hàm async khác sẽ bị dead lock.

Task.Run (và Task.Factory.StartNew ) thì có thể chạy khác thread, có thể chạy cùng thread (tùy config và ưu tiên).

Async await chỉ là keyword thôi, khi biên dịch ra thì nó thành kiểu tạo Task mới (Task.Run/Task.Factory.StartNew) rồi chạy tiếp sau await bằng Task.ContinueWith

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 20:31 04-08-2021

bạn thử xem trong đây giải thích khá chi tiết nè

DOCS

Câu hỏi mới nhất