简体   繁体   English

是否有Form.Invoke()方法?

[英]Is there a Form.Invoke() method?

I am new to multithreading (and also to C#) so I hope this is not obvious: 我是多线程(也是C#)的新手,所以我希望这不是很明显:

In my Form (WinForms application, .NET 2.0) I subscribed to an event that is raised by another object, and on handling this event I wish to change several Controls on my Form. 在我的表单(WinForms应用程序,.NET 2.0)中,我订阅了由另一个对象引发的事件,并且在处理此事件时,我希望更改表单上的几个控件。 As this event is raised in another thread than the main (UI) thread I want to marshal the call to the Form's thread. 由于此事件是在另一个线程中引发的,而不是主(UI)线程,我想封送对Form的线程的调用。

I understand I could use the Control.Invoke() method on any Control that I want to change, but as there are several of them I do not wish to do that. 我知道我可以在任何我想要更改的控件上使用Control.Invoke()方法,但由于其中有几个我不希望这样做。

When searching the internet I found hints that the Form class itself provides an Invoke() method. 在搜索互联网时,我发现提示Form类本身提供了一个Invoke()方法。 See for example: http://marioschneider.blogspot.com/2008/04/invoke-methode-fr-multithread.html (Sorry, as I am I new user I can't seem to post more than one link. I will add more links as comments if possible.) 请参阅: http//marioschneider.blogspot.com/2008/04/invoke-methode-fr-multithread.html (对不起,因为我是新用户,我似乎无法发布多个链接。我会如果可能,请添加更多链接作为评论。)

With that, I could just wrap my event handler and then use it as if it was called on the UI thread. 有了它,我可以包装我的事件处理程序,然后使用它就像在UI线程上调用它一样。 However, this doesn't seem to be defined in my environment, and in MSDN's System.Windows.Forms.Form documentation there's also no sign of it. 但是,这似乎没有在我的环境中定义,并且在MSDN的System.Windows.Forms.Form文档中也没有任何迹象。

Does this method exist in the .NET-Framework? 这种方法是否存在于.NET-Framework中? I find it hard to believe that Form would not supply such a method, as it uses the same message queue the Controls on it do. 我发现很难相信Form不会提供这样的方法,因为它使用了与它相同的消息队列。 (Or am I missing something here?) (或者我在这里遗漏了什么?)

Form 上的Invoke方法是从Control派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM