I have the following winforms event
public MainForm()
{
InitializeComponent();
new Form().ShowDialog(); // This causes the problem
}
private async void MainForm_Load(object sender, EventArgs e)
{
LoadingLabel.Text = "Initializing...";
try
{
await Task.Delay(500);
}
catch (Exception ex)
{
MessageBox.Show("Error initializing");
Environment.Exit(0);
}
Console.WriteLine(LoadingLabel.InvokeRequired);
}
Expectation: Program prints false
.
Result: Program prints true
.
It is my understanding that await should set the synchronization context back to the original and no Invoke
should be required. However, this is not the case. Attempting to update LoadingLabel
's Text
property throws an InvalidOperationException
. Am I missing something?
I am using .NET 4.5.2.