await how to prevent the next process run before the 1st process finished

Winanjaya Amijoyo 0 Reputation points
2024-05-03T04:17:23.7+00:00

Hi there,

My async code like below:

// 1st Process
if (providerId == "1") 
{ 
    await Providers.Provider1.Do_ProcessAsync(token, param1, param2); 
}

// 2nd Process
await Do_Other_Process1Async(x,y,z).ContinueWith(async (c1) => {
    await Do_Other_Process2Async(x,y,z).ContinueWith(async (c2) => {
        await Do_FinalAsync(x,y,z);
    });
});		

if the providerId met the condition, how to prevent the 2nd process run before the 1st process finished?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,246 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 4,476 Reputation points Microsoft Vendor
    2024-05-03T10:59:59.5333333+00:00

    Hi @Winanjaya Amijoyo,

    You should change your code like below.

    // 1st Process
    if (providerId == "1") 
    {
        await Providers.Provider1.Do_ProcessAsync(token, param1, param2); 
    }
    // 2nd Process
    await Do_Other_Process1Async(x, y, z);
    await Do_Other_Process2Async(x, y, z);
    await Do_FinalAsync(x, y, z);
    

    Here is my test code and test result, you also can test in your side.

    User's image

    namespace ConsoleApp1
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                string providerId = "1";
                string token = "exampleToken";
                int param1 = 10, param2 = 20;
                int x = 1, y = 2, z = 3;
                // Simulate the first process
                if (providerId == "1")
                {
                    Console.WriteLine("Start the first process...");
                    await Do_ProcessAsync(token, param1, param2);
                    Console.WriteLine("First process completed");
                }
                // Simulate the second process
                Console.WriteLine("Start the second process...");
                await Do_Other_Process1Async(x, y, z);
                await Do_Other_Process2Async(x, y, z);
                await Do_FinalAsync(x, y, z);
                Console.WriteLine("Second process completed");
            }
            static async Task Do_ProcessAsync(string token, int param1, int param2)
            {
                // Simulate asynchronous processing using Task.Delay
                await Task.Delay(3000); // Assuming processing takes 3 seconds
                Console.WriteLine("Do_ProcessAsync completed");
            }
            static async Task Do_Other_Process1Async(int x, int y, int z)
            {
                await Task.Delay(2000); // Assuming processing takes 2 seconds
                Console.WriteLine("Do_Other_Process1Async completed");
            }
            static async Task Do_Other_Process2Async(int x, int y, int z)
            {
                await Task.Delay(1000); // Assuming processing takes 1 second
                Console.WriteLine("Do_Other_Process2Async completed");
            }
            static async Task Do_FinalAsync(int x, int y, int z)
            {
                await Task.Delay(500); // Assuming processing takes 0.5 seconds
                Console.WriteLine("Do_FinalAsync completed");
            }
        }
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason

    0 comments No comments