【发布时间】:2017-08-10 10:07:56
【问题描述】:
我得到了一个
跨线程操作无效
... 下面的代码出错。我认为 async/await 会解决这个问题,但显然不是。奇怪的是,当我注释掉textBoxUser.Enabled = false 时,错误消失了。想法?
private async void buttonPopulate_Click(object sender, EventArgs e)
{
textBoxUser.Enabled = false;
await Populate();
textBoxUser.Enabled = true; //error here
}
【问题讨论】:
-
您正试图从不同的线程访问一个 GUI 元素。你需要一个调用。
-
private async void buttonPopulate_Click如果 WinForms 正在调用此事件处理程序,您应该在 WinForms 同步上下文中并在主线程上继续。您是自己调用此方法还是从计时器调用此方法,还是实际调用await Populate().ConfigureAwait(false);? -
@MongZhu 略有不同的问题,上面的答案已经过时了
-
1-发布 Populate 的代码,因为可能存在问题。 2-发布您获得异常的行。
-
Populate在做什么?你能用a simple example 复现吗?
标签: c# multithreading asynchronous async-await vsto