As I know, asynchronous functions return immediately. The sample code below confirms me.
static void Main(string[] args)
{
StartTasks2();
Console.ReadKey();
}
private static void StartTasks2()
{
Console.WriteLine($"Task2 Started: {DateTime.Now:s.ffff}");
AddPersonFake();
Console.WriteLine($"Task2 Finished: {DateTime.Now:s.ffff}");
}
private static async void AddPersonFake()
{
Console.WriteLine($"AddPersonFake Started: {DateTime.Now:s.ffff}");
await Task.Delay(2000);
Console.WriteLine($"AddPersonFake Finished: {DateTime.Now:s.ffff}");
}
But this code is not working (I'm waiting for the result as above):
static void Main(string[] args)
{
StartTasks1();
Console.ReadKey();
}
private static void StartTasks1()
{
Console.WriteLine($"Task1 Started: {DateTime.Now:s.ffff}");
AddPerson();
Console.WriteLine($"Task1 Finished: {DateTime.Now:s.ffff}");
}
private static async void AddPerson()
{
Console.WriteLine($"AddPerson Started: {DateTime.Now:s.ffff}");
using ( TempDbContext context = new TempDbContext() )
{
context.Persons.Add(new Person());
await context.SaveChangesAsync();
}
Console.WriteLine($"AddPerson Finished: {DateTime.Now:s.ffff}");
}
How can I return immediately from AddPerson()?

