c#编程中的回调项 [英] Callback term in c# programming

查看:77
本文介绍了c#编程中的回调项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我看到一些方法或委托他们的名字符合XXX +回调模式

。什么意思是回调?

例如CustomPopupPlaceentCallback委托。

解决方案

wiki [ ^ ]。



在C#中,委托回调在需要时执行。


imho回调 的关键方面是:可执行文件代码以一种形式写入,可以传递作为参数,然后执行代码。



通常将回调中的代码执行称为调用。



不同的语言有所不同<我>他们如何实现这个功能:例如,一个共同的描述.NETworld之外的iption将:传递指向函数的指针。但是,.NET的目的不是明确使用指针。



请记住,.NET没有正式区分术语函数和方法 ,就像许多其他语言一样。



描述回调操作行为的另一个常用术语是:延迟执行。在.NET中,您可以将创建Delegates视为一种封装可执行代码的方法,然后将其移交到使用该代码的其他方法。



I认为一个例子可能有用:



假设:你在Form上有两个TextBox,'textBox1',textbox2,一个Button,'SomeButton

  private   void  SomeButton_Click( object  sender ,EventArgs e)
{
// 使用方便的'Func符号
// 并使用lambda表示法定义参数
// 和函数体
// 以两个整数作为参数并返回一个字符串
Func< int, int ,string> ReturnStringCallBack =(x,y)= >
{
return textBox1。文本
+ + x.ToString()
+ + textBox2.Text
+ + y.ToString();
};

// 调用'executeFunc传递参数和指向回调的指针
string result = executeFunc( 100 200 ,ReturnStringCallBack);

// 显示延迟执行ReturnStringCallBack的结果
MessageBox.Show(result);
}

private string executeFunc( int x, int y,Func< int, int ,string> ; theCallBack)
{
// 暂停4秒
System.Threading.Thread.Sleep( 4000 );

// 使用提供的参数值调用回调
return theCallBack(x,y);
}

这里的关键是你明白你可以根据需要组成一个可执行代码单元,并将它传递给一些外部方法/上下文,以便延迟,随时,执行(调用) ),并且代码单元的延迟执行控件将返回到回调发起的方法/上下文。



如果你的delegate(可执行代码的单元)返回一个值,就像使用'Func表示法创建的任何委托一样,然后你可以使用定义委托的代码中返回的值。



为了创建一个代码单元(委托)作为不返回值的参数传递,你可以使用'动作符号。


回调就是这样的东西传递给另一段代码,以便另一段代码可以在主叫方面做一些事情。好吧,对于一个非常简单的想法,这是很多令人困惑的措辞。也许一个例子会有所帮助:



  • 我有一个计算工资的花哨的应用程序。我的代码在服务器A上调用了一个执行所有处理的服务,我希望代码在完成后给我一个更新的工资值。

    • 我希望这段代码是开始并被遗忘 - 因为,我的意思是我只想打电话给服务然后继续做其他事情。我不想等待代码完成处理。
  • 通过将回调传递给该代码,它可以告诉我更新的工资值是什么时候
  • 我的应用程序现在不必等待服务完成 - 我可以运行我的应用程序,因为它知道它会在某些时候向我发送更新。

Hi there . i see some methods or delegate that their name conforms XXX+"Callback" pattern
.what does mean Callback ?
for example CustomPopupPlaceentCallback delegate.

解决方案

Definition of callback on wiki[^].

In C#, delegate callback execute when they are needed.


imho the key aspect of what a "callback" is ... is: executable code that is written in a form where it can be passed as a parameter to some other method which can then execute the code.

It is typical to refer to the execution of the code in a callback as "invocation."

Different languages vary in how they implement this functionality: for example, a common description outside the .NET "world" would be: to pass a pointer to a function. But, .NET was designed not to make explicit use of pointers.

Keep in mind that .NET doesn't formally distinguish between the terms "function" and "method," as many other languages do.

Another frequent term in describing the operational behavior of callbacks is: deferred execution. In .NET you can think of creating Delegates as being a way to encapsulate executable code, and then "hand it off" to other methods that use the code.

I think an example may be useful:

Assume: you have two TextBoxes on a Form, 'textBox1, 'textbox2, a Button, 'SomeButton

private void SomeButton_Click(object sender, EventArgs e)
{
    // define a delegate using the convenient 'Func notation
    // and using lambda notation to define the parameters
    // and function body
    // that takes two integers as parameters and returns a string
    Func<int, int, string> ReturnStringCallBack = (x, y) =>
    {
            return textBox1.Text
            + " " + x.ToString()
            + " " + textBox2.Text
            + " " + y.ToString();
    };

    // call 'executeFunc passing parameters and a pointer to the callback
    string result = executeFunc(100, 200, ReturnStringCallBack);

    // show the result of the deferred execution of ReturnStringCallBack
    MessageBox.Show(result);
}

private string executeFunc(int x, int y, Func<int, int, string> theCallBack)
{
    // pause for four seconds
    System.Threading.Thread.Sleep(4000);

    // invoke the callback using the supplied parameter values
    return theCallBack(x,y);
}

The key thing here is that you understand that you can compose a unit of executable code as needed and pass it to some external method/context for deferred, any-time, execution (invocation), and that after the unit of code's deferred execution control will return to the method/context in which the callback originated.

If your delegate (unit of executable code) returns a value, as any delegate created using the 'Func notation does, then you can use the value returned in the code that defined the delegate.

For creating a unit of code (delegate) to be passed as a parameter that does not return a value, you can use the 'Action notation.


A callback is simply something that is passed to another piece of code so that the other piece of code can do something back at the calling side. Okay, that's a lot of very confusing wording for a very simple idea. Perhaps an example will help:

  • I've got my fancy application that calculates salaries. My code calls a service on Server A that does all the processing, and I want that code to give me an updated salary value when it's finished.
    • I want this code to be kicked off and forgotten about - by that, I mean that I want to just call the service and then carry on doing other things. I don't want to wait for that code to finish processing.
  • By passing a callback to that code, it can tell me what the updated salary value is when it is ready.
  • My application now doesn't have to wait for the service to finish - I can run my app happy in the knowledge that it will fire an update back to me at some point.


这篇关于c#编程中的回调项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆