-1

Suppose I am giving some tasks in the main method

public static void Main(string[] args)
{
   // Synchronous methods
   Task1();
   Task2();
   Task3();
   Task4();

   // Asynchronous methods
   Task5();
   Task6();
   Task7();
   Task8();
}

Now, I going to extend all methods,

public static void Task1()
{
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 1...");
   Task.Delay(4000).Wait();
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 1!");
}

public static void Task2()
{
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 2...");
   Task.Delay(2000).Wait();
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 2!");
}

public static void Task3()
{
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 3...");
   Task.Delay(5000).Wait();
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 3!");
}

public static void Task4()
{
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 4...");
   Task.Delay(1000).Wait();
   Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 4!");
}

public static async void Task5()
{
   await Task.Run(() =>
     {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 5...");
        Thread.Sleep(4000);
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 5!");
     });
}

public static async void Task6()
{
   await Task.Run(() =>
     {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 6...");
        Thread.Sleep(2000);
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 6!");
     });
}       

public static async void Task7()
{
   await Task.Run(() =>
     {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 7...");
        Thread.Sleep(5000);
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 7!");
     });
}

public static async void Task8()
{
   await Task.Run(() =>
     {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Started task 8...");
        Thread.Sleep(1000);
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " : " + "Completed task 8!");
     });
}

Here, the first four tasks are synchronous methods that work perfectly. But the last four tasks are asynchronous methods that do not give me actual output. Asynchronous methods are started but never completed. Why? [I use Task.Delay() for the first four synchronous methods and Thread.Sleep() for the last four asynchronous methods]

Here is my code click here

I want an answer like this,

11:25:26 AM : Started task 1...
11:25:30 AM : Completed task 1!
11:25:30 AM : Started task 2...
11:25:32 AM : Completed task 2!
11:25:32 AM : Started task 3...
11:25:37 AM : Completed task 3!
11:25:37 AM : Started task 4...
11:25:38 AM : Completed task 4!
------------------
11:25:38 AM : Task 5 starting...
11:25:38 AM : Task 6 starting...
11:25:38 AM : Task 7 starting...
11:25:38 AM : Task 8 starting...
11:25:39 AM : Task 8 completed!
11:25:40 AM : Task 6 completed!
11:25:42 AM : Task 5 completed!
11:25:43 AM : Task 7 completed!
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 2
    Look at the top 3 **Related** questions on the right of this page. Did you even bother to read any of that? – H H Jul 16 '23 at 06:32
  • 1
    @HH the OP could not see the related questions before posting their own question. Are you upset because they don't own a time-machine, or some other cause-and-effect-inversion device? – Theodor Zoulias Jul 16 '23 at 06:50
  • 1
    @HH Top 3 related questions on the right of this page are not the same as I asked! – Showyeab Ahmed Jul 16 '23 at 06:54
  • 1
    I modified your code, look at https://onlinegdb.com/SjO9cm67B – Ihdina Jul 16 '23 at 13:29
  • @Ihdina Thank you for your hard work! But I know that task 8 is only 1 second. So task 8 will run first, then task 6, task 5, and finally task 7. But why is this not happening in that asynchronous method on your code? – Showyeab Ahmed Jul 17 '23 at 15:08
  • 1
    @ShowyeabAhmed, look at https://onlinegdb.com/3v4X7YJHT – Ihdina Jul 18 '23 at 04:40
  • @Ihdina Wow you are a great programmer. I wanted this answer. Thank you – Showyeab Ahmed Jul 18 '23 at 05:18

1 Answers1

4

They're never completed because Main exits before they're done.

By making them async void, you've already told the compiler that you don't care when (or even if) they end. If you actually care about that, make them async Task, and await those tasks.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111