DEV Community

ConnectSaravana
ConnectSaravana

Posted on

Async and Await

using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        // Start multiple event loops asynchronously
        Task eventLoop1 = EventLoopAsync("Event Loop 1");
        Task eventLoop2 = EventLoopAsync("Event Loop 2");

        // Continue with other tasks
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Main Loop: Doing some other work...");
            await Task.Delay(1000); // Simulate other work
        }

        // Wait for event loops to complete
        await Task.WhenAll(eventLoop1, eventLoop2);

        Console.WriteLine("All event loops completed.");
    }

    static async Task EventLoopAsync(string name)
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine($"{name}: Iteration {i}");

            // Pause the event loop for some time
            await Task.Delay(2000); // Simulate async operation

            Console.WriteLine($"{name}: Resuming after pause");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In this code example, we have the Main method as the entry point of our program. Inside Main, we start two event loops asynchronously by calling the EventLoopAsync method twice. These event loops are represented by the eventLoop1 and eventLoop2 tasks.

After starting the event loops, we proceed with some other tasks represented by the for loop. In this case, we simulate other work by printing a message and waiting for 1 second using await Task.Delay(1000).

While the Main loop is executing other work, the event loops are running concurrently. Each event loop runs its own iteration, printing messages and simulating pauses with await Task.Delay(2000).

The key point to note here is that the event loops are asynchronous, which means when the await Task.Delay(2000) is encountered within an event loop, it pauses the execution of that specific event loop. However, the event loop is only paused within itself, not affecting the progress of other event loops or the Main loop.

As a result, while one event loop is paused and waiting for the delay to complete, other event loops or the Main loop continue their execution. This allows the program to handle multiple tasks concurrently without blocking.

Once the delay is complete, the event loop resumes its execution, and the next iteration begins. This continues until all iterations of the event loops are completed.

In the Main method, we use await Task.WhenAll(eventLoop1, eventLoop2) to wait for both event loops to complete before proceeding. Finally, a message is printed indicating that all event loops have completed.

Top comments (0)