.net 中 Invoke() 有什么用?

发布于 2024-07-14 02:58:53 字数 155 浏览 5 评论 0原文

我找到了这段代码:

this.Invoke(new EventHandler(EventGetSum));

这和写:

EventGetSum();

这有什么用?

I found this code:

this.Invoke(new EventHandler(EventGetSum));

Is this not the same as writing:

EventGetSum();

What's the use of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我ぃ本無心為│何有愛 2024-07-21 02:58:53

如果您编写 EventGetSum(),它会立即调用 EventGetSum 方法。

如果您编写 new EventHandler(EventGetSum) 来创建一个委托,该委托将在调用时调用 EventGetSum。

调用 Control.Invoke从负责控件的 UI 线程调用给定的委托。 这是必要的,因为您不能从任意线程访问 UI 元素。

If you write EventGetSum() that immediately calls the EventGetSum method.

If you write new EventHandler(EventGetSum) that creates a delegate which will (in turn) call EventGetSum when it's invoked.

The call to Control.Invoke invokes the given delegate from the UI thread responsible for the control. This is necessary because you mustn't access UI elements from arbitrary threads.

芸娘子的小脾气 2024-07-21 02:58:53

它在this 窗口所属的线程中执行EventGetSum 方法。

It executes the EventGetSum method in the thread to that the window this belongs to.

够运 2024-07-21 02:58:53

这通常在处理跨线程 UI 调用时使用。

查看 ISynchronizeInvoke 的 MSDN 文档。

This is normally used when dealing with cross thread UI calls.

Look at the MSDN documentation for ISynchronizeInvoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文