Which C# assembly contains Invoke?

孤街浪徒 提交于 2019-12-05 08:58:36
djdd87

Invoke is within Control. I.e. Control.Invoke();

There's no way to call Invoke directly as there's no such method in System.Windows.Forms. The Invoke method is a Control Member.

Here's 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);
    }
}

You need to call Invoke on an instance of something which contains it - if you're using Windows Forms, that would be a control:

control.Invoke(someDelegate);

or for code within a form, you can use the implicit this reference:

Invoke(someDelegate);

You shouldn't need to go through any particular hoops. If Visual Studio is complaining, please specify the compiler error and the code it's complaining about. There's nothing special about Invoke here.

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

txtBox.Invoke(...);

It can also be accessed via an interface, or sync-context if you want abstraction - but the easiest approach is to handle it at the UI via an event, in which case controls are conveniently available.

If you really want to become the worlds best c# programmer you have to learn that threads are not a good thing unless used correctly.

Updating the UI across threads is usually a sign that you are abusing threads.

Anyways, it's not enough to use using System.Windows.Forms, you have to add it to the references. Make a right-click on References in your project explorer, then Add References and select System.Windows.Forms

Invoke is a method on objects, usually found on the Controls in the Forms library and some async classes. You of course need specific objects to be able to call Invoke on that control/class.

You're presumably trying to call Invoke from within a class (i.e. not from within a Form or a Control). Move your code out of the class and into a 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 as well since it assumes the this).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!