I have the following prolblem from an existing code base: The AsyncAwaitQuestion1 calls an async method inside a Task.Run, from the caller(my code), I need to wait for the underlying private async method to be completed. How is a clean/not-so-dirty way to accomplish this?
see below
public class AsyncAwaitQuestion1
{
public List<string> MyList;
public AsyncAwaitQuestion1()
{
MyList = new List<string>() { "a", "b" };
Task.Run(() => DoSomethingAsync());
}
private async Task DoSomethingAsync()
{
await Task.Delay(new TimeSpan(0, 0, 0, 0, 10));
await Task.Delay(new TimeSpan(0, 0, 0, 2));
await Task.Delay(new TimeSpan(0, 0, 0, 3));
MyList.Add("c");
await Task.Delay(new TimeSpan(0, 0, 0, 4));
MyList.Add("d");
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
var question = new AsyncAwaitQuestion1();
int x = 0;//I would like to read all the 4 elements here
}