我在UI上遇到跨线程问题。我已经阅读了所有的方法来实现它,并且已经实现了它们,如下所示。跨线程UI
public void UpdateList(object obj)
{
// do we need to switch threads?
if (listBox1.InvokeRequired)
{
MethodInvoker del =() => UpdateList(obj);
this.Invoke(del);
return;
}
// ok so now we're here, this means we're able to update the control
// so we unbox the object into a string
string text = (string)obj;
// and update
listBox1.Items.Add(text);
}
问题是当我尝试做一个
hubConnection.Start().Wait();
该呼叫我试图更新我的列表之后。
没有等待很好。当我添加Wait时,它挂在UpdateList Invoke上。没有错误......它只是挂起。
我在按钮事件中处理此调用。
hubConnection.Start()。等待()创建主线程死锁。删除等待和看到 – lboshuizen 2014-09-19 15:51:40
简单的规则要记住:使用调用错误99%,总是使用BeginInvoke。开始一个线程并等待它是100%错误,根本不需要线程。使用InvokeRequired是95%错误,你已经知道它是从一个线程调用的。把它们写下来,钉在墙上,让你摆脱困境。 – 2014-09-20 11:48:42