简体   繁体   English

有许多不同的调用方法?

[英]Many different Invoking methods?

I don´t really understand the Invoking. 我不太了解调用。 I get that what it´s doing is that it tells that current action to be run on the thread it needs to be ran on (UI Thread). 我知道它的作用是告诉当前操作要在需要运行的线程(UI线程)上运行。 But what i don´t get is why you can do so many different versions. 但是我不明白的是为什么您可以做这么多不同的版本。 for example: 例如:

this.BeginInvoke(new MethodInvoker(delegate { currping.Text = Status; }));

this.BeginInvoke(new Action(() => {  currping.Text = Status; }));

I don´t understand if there is a difference. 我不知道是否有区别。 They will both do the same thing from what i can grasp. 根据我的理解,他们俩都会做同样的事情。

Am i missing something here? 我在这里想念什么吗?

Action was added in the .NET 3.5 framework as a general delegate that takes no arguments and returns nothing, while MethodInvoker has existed since 1.1, so you'll typically only see older code using MethodInvoker . 在.NET 3.5框架中, Action被添加为不带任何参数且不返回任何内容的常规委托,而MethodInvoker自1.1开始就存在,因此通常仅使用MethodInvoker才能看到较旧的代码。 The two delegate types have the same signature. 这两种委托类型具有相同的签名。

BeginInvoke() actually takes a Delegate object, so any delegate that takes no arguments can be used with that overload. BeginInvoke()实际上接受一个Delegate对象,因此任何不带参数的委托都可以与该重载一起使用。 (There is a (Delegate, object[]) overload for delegates that need arguments, though I'd be inclined to use a no-argument closure instead.) (对于需要参数的(Delegate, object[])有一个(Delegate, object[])重载,尽管我倾向于使用无参数的闭包。)

Because Delegate is a general type, you must specify in some way what type of delegate you are providing because the compiler cannot otherwise infer it (see below). 因为Delegate是一种通用类型,所以您必须以某种方式指定要提供的委托类型,因为编译器无法以其他方式推断出它(请参阅下文)。 This is shorter than the new Action alternative, but only by a few characters and it isn't really any more or less clear: 这比new Action替代方法要短,但只有几个字符,实际上一点也不清晰:

this.BeginInvoke((Action)(() => { ... }));

You have to specify a delegate type here because the compiler doesn't know which delegate type you want. 您必须在此处指定委托类型,因为编译器不知道您想要哪种委托类型。 For example: 例如:

Action foo = () => { };

This works because the compiler can infer based on the type of foo that the delegate type should be Action . 之所以可行,是因为编译器可以根据foo的类型推断委托类型应该为Action

// All of these examples cause CS1660.
Delegate foo1 = () => { };
object foo2 = () => { };
var foo3 = () => { };

These are compile-time errors because there are many delegate types that match the signature "takes no arguments and returns void " and the compiler doesn't pretend to know which you wanted. 这些是编译时错误,因为有很多与签名“不带参数并返回void ”匹配的委托类型,并且编译器不假装知道您想要哪个。 (And in the object and var cases, you may also have meant an Expression<> object, which isn't a delegate at all. Nevertheless, you'd get the same error if you used delegate () {} instead of a lambda expression.) (并且在objectvar情况下,您可能还意味着一个Expression<>对象,它根本不是委托。但是,如果使用delegate () {}而不是lambda,则会出现相同的错误。表达。)

There are two pieces to this the new Something( part and the part inside the inner most parenthesis. new Something(部分和最里面的括号内的部分)分为两部分。

Both MethodInvoker and Action are Delegates , you need to think of them like "Function pattern" definitions. MethodInvokerAction都是Delegates ,您需要将其视为“功能模式”定义。 What both of those two "patterns" represent is "a function that accepts no arguments and returns void" 这两个“模式”都表示的是“一个不接受任何参数并返回void的函数”

namespace System.Windows.Forms
{
    public delegate void MethodInvoker();
}

namespace System
{
    public delegate void Action()
}

So they both represent the exact same "pattern" it is just defined in two places for coding convenience. 因此,它们都代表了完全相同的“样式”,为方便编码,仅在两个地方定义了“样式”。 For example it is more obvious that you are planning on using the delegate to invoke a method for a Winforms Form using MethodInvoker than using Action . 例如,很明显,与使用Action相比,您计划使用委托通过MethodInvoker调用Winforms Form的方法。

the 2nd part is the stuff inside the parenthesis. 第二部分是括号内的内容。 Both of those two definitions mean the same thing, however the pattern () => { ... } is a Lambada Expression and was not added till visual studio 2008 or newer. 这两个定义的含义相同,但是模式() => { ... }Lambada表达式 ,直到Visual Studio 2008或更高版本才添加。 They both mean is just "I am defining a function here inline", this creates what is called an Anonymous Function because it is a function with no name. 它们都意味着“我在此内联定义函数”,这创建了一个所谓的匿名函数,因为它是一个没有名称的函数。 You also could pass a function name in instead 您也可以传入一个函数名

public void Foo()
{
    this.BeginInvoke(new Action(Bar)); //BeginInvoke will call the function Bar()
}

private void Bar()
{
}

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

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