gpt4 book ai didi

c# - 为什么我应该使用 methodinvoker 而不是声明一个 void 方法并调用它?

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:50 24 4
gpt4 key购买 nike

我正在使用异步套接字操作,当客户端收到一条消息时,它使用以下代码调用一个方法并执行 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com