The normal behavior of using await to call an asynchronous method is that its context stays the same as shown in the example below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
//main thread here
await SampleAsync();
//stays as main thread
var g = 10;
}
static async Task SampleAsync()
{
await Task.Delay(1000);
}
}
But when I use this in the Winforms Main console method call, this behavior is no longer true: After calling the await method, the thread change from the main thread to the worker thread. Why is this? I would like for it to stay on the Main (UI Thread) after the await call.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainAsync().Wait();
}
static async Task MainAsync()
{
Application.Run(new Form1());
//Main Thread here
await Task.Delay(1000);
//Why does it turn in to worker thread here? ConfigureAwait(false) is not used here?!
var g = 5;
//Run other Application.Run() if certain conditions apply; but I need to be in the Main Thread.
}
In case you are wondering what I am trying to achieve, I am trying to catch the errors in the async method, MainAsync, rather than Main so that I can avoid having to disentangle errors from the AggregateException (https://msdn.microsoft.com/en-us/magazine/JJ991977.aspx; Figure4). I also want to stay in the UI thread so I can run other Application.Run in the UI thread.