I am getting a
Cross-thread operation not valid
... error with the code below. I thought async/await would take care this, but apparently not. Weird thing is that when I comment out textBoxUser.Enabled = false
, the error disappears. Thoughts?
private async void buttonPopulate_Click(object sender, EventArgs e)
{
textBoxUser.Enabled = false;
await Populate();
textBoxUser.Enabled = true; //error here
}
Your problem is probably due to VSTO not properly providing a SynchronizationContext
; this is a long-standing problem with Office plugin systems. You can verify this by checking the value of SynchronizationContext.Current
at the beginning of your event handler; if it is null
, then the problem is due to VSTO.
To fix this, you can do this at the beginning of any async void
event handlers:
SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.