What C # assembly contains Invoke?

Alternative question: why is VS10 so eager to complain about Invoke?

In my ongoing quest to get my application working to become a better C # programmer, I decided that threads are Good Thing ™.

MSDN has a useful article on creating thread-safe calls for controls , but it (and, it would seem, every other article on this topic) refers obliquely to the Invoke method. Sometimes even the BeginInvoke which I read will be preferable .

All of this would be great if I could get a visual studio for Invoke recognition. MSDN says it is contained in the System.Windows.Forms assembly , but I already “use” it. Of course, I also tried to use System.Threading, but to no avail.

What hoops do I need to jump to make invoke work?

+5
multithreading c # invoke visual-studio-2010
Jun 24 '10 at 11:53 on
source share
6 answers

Invoke is within the control. That is, Control.Invoke();

It is not possible to invoke Invoke directly, since there is no such method in System.Windows.Forms . The Invoke method is a control.

Here is an example I made earlier:

 public delegate void AddListViewItemCallBack(ListView control, ListViewItem item); public static void AddListViewItem(ListView control, ListViewItem item) { if (control.InvokeRequired) { AddListViewItemCallBack d = new AddListViewItemCallBack(AddListViewItem); control.Invoke(d, new object[] { control, item }); } else { control.Items.Add(item); } } 
+4
Jun 24 2018-10-06T00:
source share

You need to call Invoke on the instance of something that contains it - if you use Windows Forms, it will be a control:

 control.Invoke(someDelegate); 

or for code inside a form, you can use the implicit this link:

 Invoke(someDelegate); 

You do not need to go through any special hoops. If Visual Studio complains, indicate the compiler error and the code it complains about. There is nothing special about Invoke .

+2
Jun 24 2018-10-06T00:
source share

Winform Invoke is a Control instance method - you just need an instance of the control (which in many cases can be this ). For example:

 txtBox.Invoke(...); 

It can also be accessed through an interface or a synchronizing context if you want an abstraction, but the easiest approach is to process it in the user interface through an event, in which case the controls are available.

+2
Jun 24 2018-10-06T00:
source share

If you really want to become a better C # worlds programmer, you need to find out that threads are not very good if they are not used correctly.

Updating the user interface for threads is usually a sign that you are abusing threads.

In any case, this is not enough to use using System.Windows.Forms , you must add it to the links. Right-click on References in the project explorer, then add links and select System.Windows.Forms

+1
Jun 24 2018-10-06T00:
source share

Invoke is a method for objects commonly found in controls in the Forms library and some async classes. Of course, you need specific objects in order to be able to invoke Invoke on which controls / class.

0
Jun 24 2018-10-06T00:
source share

Presumably you are trying to call Invoke from a class (i.e. not from Form or Control ). Move your code from the class to the form or control, and you will see that Invoke compiles and works correctly (strictly speaking, your code should reference this.Invoke , which makes the source of the method clear, but Invoke will work just as intended this ).

0
Jun 24 2018-10-12T00:
source share



All Articles