我正在使用异步套接字操作,当客户端收到一条消息时,它使用以下代码调用一个方法并执行 Methodinvoker:
private void AddText(string message)
{
MethodInvoker mi = new MethodInvoker(delegate
{
textBox1.Text += message;
});
this.Invoke(mi);
}
我不明白为什么要使用 methodinvoker。 最好使用它而不是声明和调用不带参数的 void 方法?一定有充分的理由。
最佳答案
显然你的方法是从另一个线程调用的,然后是你的主线程。
Invoke :
Executes the specified delegate on the thread that owns the control's underlying window handle.
您应该始终在主线程上与您的用户控件进行交互。 Invoke
方法将委托(delegate)发送回应用程序的主线程。
关于c# - 为什么我应该使用 methodinvoker 而不是声明一个 void 方法并调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734950/