How to call on async Task function from Void class?
Let's say I got this built up like this,
I Want to call the methods in Run() in sync, but in the DoSomeStuffAsync and DoSomeStuffAsync 2 It requires await on some functions that has async awaitable functions.
My Question is, how can I in DoTask1 e.g. make the task to run sync? Like now the tasks just starts it and it doesn't wait until it finish at all.
static void Main(string[] args)
{
Run();
}
public void Run()
{
DoTask1();
DoTask2();
}
public void DoTask1()
{
var tasks = Task.Run(async () =>
{
await DoSomeStuffAsync();
await DoSomeStuffAsync2();
});
Task.WaitAll(tasks);
}
public void DoTask2()
{
var tasks = Task.Run(async () =>
{
await DoSomeStuffAsync();
await DoSomeStuffAsync2();
});
Task.WaitAll(tasks);
}
private async Task DoSomeStuffAsync(int daysBack)
{
try
{
var data= await GetData();
var objj = GenerateObjects();
await InsertToDb(objj ));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private async Task DoSomeStuffAsync2(int daysBack)
{
try
{
var data= await GetOtherData();
var objj = GenerateObjects();
await InsertToDb(objj ));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}