1
我得到一个跨线程操作无效使用异步/等待在VSTO
跨线程操作无效
...错误与下面的代码。我认为异步/等待会照顾这一点,但显然不是。奇怪的是,当我注释掉textBoxUser.Enabled = false
时,错误消失。思考?
private async void buttonPopulate_Click(object sender, EventArgs e)
{
textBoxUser.Enabled = false;
await Populate();
textBoxUser.Enabled = true; //error here
}
你正试图从不同的线程访问GUI元素。你需要一个调用。 – Trey
'private async void buttonPopulate_Click'如果WinForms调用此事件处理程序,则应该在WinForms同步上下文中并继续在主线程上。你是自己调用这个方法还是从一个定时器调用这个方法,或者实际调用'await Populate()。ConfigureAwait(false);'? –
@MongZhu稍微不同的问题和最新的答案已过时 – MickyD