I want to call step2 method after step 1 is finished. With this code below the methods step 1 and step 2 are executing in parallel. And of course I they need to be asynchronous so they won't block step 3 from executing.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace test
{
internal class Program
{
static void Main(string[] args)
{
async void step1()
{
await Task.Run(() =>
{
Thread.Sleep(2000);
Console.WriteLine("step 1");
});
}
async void step2()
{
await Task.Run(() =>
{
Thread.Sleep(5000);
Console.WriteLine("step 2");
});
}
void step3()
{
Console.WriteLine("step 3");
}
step1();
step2();
step3();
// the code below is not working but just and idea
// what I want to make. If it at all posible
// step1().step2()
// step3()
Console.ReadLine();
}
}
}
Any help would be greatly appreciated. Keep in mind I am beginner in C#!
Edit:
I know that I can get the end result even with this code.
void step1() { Thread.Sleep(2000); Console.WriteLine("step 1"); } void step2() { Thread.Sleep(5000); Console.WriteLine("step 2"); } void step3() { Console.WriteLine("step 3"); } step3(); step1(); step2();
even without async/await at all.
The point of this question is to make small proof of concept application where even though the code is set up like this:
step1(); step2(); step3();
where step3() is set last will execute first because has no delay and also step2() will have to wait for step1() to finish.
Is this at all possible with async/await in C#. I think this can be done with promises in javascript.