none
Running two routine by Task.Run at same time RRS feed

  • Question

  • see my first set of code.

            private async void button1_Click(object sender, EventArgs e)
            {
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
            }
    
            
            public string GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
                Thread.Sleep(1000);
                return "cat found";
            }
    
            public string GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                Thread.Sleep(2000);
                return "food found";
            }

    in the above code i am running two routine at same time by Task.Run. the above two routine is not asynchronous rather synchronous.

    in later code set of code i make the above two routine asynchronous and run those function parallel by task.run

            private async void button1_Click(object sender, EventArgs e)
            {
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
            }
    
            public async Task<string> GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
                await Task.Delay(1000);
                return "cat found";
            }
    
            public async Task<string> GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                await Task.Delay(2000);
                return "food found";
            }
     

    most important see my two set of code and tell me what would be difference calling two synchronous method by task.run and again calling two asynchronous method by task.run ?

    now tell me which approach is good and would be efficient and why ? task.run spawn a new thread ?

    async await also create new thread ?

    please guide me.



    • Edited by Sudip_inn Sunday, December 23, 2018 9:49 AM
    Sunday, December 23, 2018 9:33 AM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    >>in the above code i am running two routine at same time by Task.Run. the above two routine is not asynchronous rather synchronous.

    For your question, I make a simple example to show the results, the two is asynchronous rather synchronous. Both of the two method would create a new thread.

    For better understanding, I output the result in output Window.

       private async void button1_ClickAsync(object sender, EventArgs e)
            {
    
                Console.WriteLine("Main Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
    
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
    
            }
    
    
            public static string GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
    
                Thread.Sleep(1000);
                Console.WriteLine("GetAllTheCats Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "cat found";
            }
    
            public static string GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                Thread.Sleep(2000);
                Console.WriteLine("GetAllTheFood Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "food found";
            }

    >>in later code set of code i make the above two routine asynchronous and run those function parallel by task.run

    I test the code your provided.

     private async void Button1_ClickAsync(object sender, EventArgs e)
            {
    
                Console.WriteLine("Main Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
            }
    
            public async Task<string> GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
    
                await Task.Delay(1000);
                Console.WriteLine("GetAllTheCats Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "cat found";
            }
    
            public async Task<string> GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                await Task.Delay(2000);
    
                Console.WriteLine("GetAllTheFood Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "food found";
            }

    The two methods are asynchronous methods. We use async await to implement synchronization in asynchronous.

    If you want to achieve parallel, you could use Parallel instead of Task.Run.

    For more details about Parallel, please refer to the MSDN document.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 6:27 AM
    Moderator

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    >>in the above code i am running two routine at same time by Task.Run. the above two routine is not asynchronous rather synchronous.

    For your question, I make a simple example to show the results, the two is asynchronous rather synchronous. Both of the two method would create a new thread.

    For better understanding, I output the result in output Window.

       private async void button1_ClickAsync(object sender, EventArgs e)
            {
    
                Console.WriteLine("Main Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
    
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
    
            }
    
    
            public static string GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
    
                Thread.Sleep(1000);
                Console.WriteLine("GetAllTheCats Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "cat found";
            }
    
            public static string GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                Thread.Sleep(2000);
                Console.WriteLine("GetAllTheFood Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "food found";
            }

    >>in later code set of code i make the above two routine asynchronous and run those function parallel by task.run

    I test the code your provided.

     private async void Button1_ClickAsync(object sender, EventArgs e)
            {
    
                Console.WriteLine("Main Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                var task1 = Task.Run(() => GetAllTheCats());
                var task2 = Task.Run(() => GetAllTheFood());
                var result = await Task.WhenAll(task1, task2);
            }
    
            public async Task<string> GetAllTheCats()
            {
                // Do stuff, like hit the Db, spin around, dance, jump, etc...
                // It all takes some time.
    
                await Task.Delay(1000);
                Console.WriteLine("GetAllTheCats Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "cat found";
            }
    
            public async Task<string> GetAllTheFood()
            {
                // Do more stuff, like hit the Db, nom nom noms...
                // It all takes some time.
                await Task.Delay(2000);
    
                Console.WriteLine("GetAllTheFood Thread ID:  " + Thread.CurrentThread.ManagedThreadId);
                return "food found";
            }

    The two methods are asynchronous methods. We use async await to implement synchronization in asynchronous.

    If you want to achieve parallel, you could use Parallel instead of Task.Run.

    For more details about Parallel, please refer to the MSDN document.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 24, 2018 6:27 AM
    Moderator
  • private async void button1_Click(object sender, EventArgs e)
            {
                var task1 = Task.Run(() => UpdateLabel1());
                var task2 = Task.Run(() => UpdateLabel2());
                var result = await Task.WhenAll(task1, task2);
            }
    
            public async Task<bool> UpdateLabel1()
            {
                await Task.Run(() =>
                {
                    for (var i = 0; i <= 200; i++)
                    {
                        BeginInvoke((Action)(() =>
                        {
                            label1.Text = i.ToString();
    
                        }));
    
                        Thread.Sleep(100);
                    }
                });
    
                return true;
            }
    
            public async Task<bool> UpdateLabel2()
            {
                await Task.Run(() =>
                {
                    for (var i = 0; i <= 100; i++)
                    {
                        BeginInvoke((Action)(() =>
                        {
                            label2.Text = i.ToString();
    
                        }));
    
                        Thread.Sleep(100);
                    }
                });
                return true;
            }

    see this line await Task.WhenAll(task1, task2);

    what is the meaning of the above line of code ?

    i thought task.whenall will start the execution of two function at same time  Task.WhenAll(task1, task2); am i wrong ?

    how to execute multiple function by Task library ?

    why you said  use Parallel instead of Task.Run. can't we run multiple function in parallel with Task ?

    Saturday, January 12, 2019 5:35 PM