什么是callback?

什么是callback,它是如何在C#中实现的?

在计算机编程中 ,callback是作为parameter passing给其他代码的可执行代码。

– 维基百科:回拨(计算机科学)

C#有这个目的的代表 。 它们大量使用事件 ,因为事件可以自动调用多个附加委托(事件处理程序)。

我刚认识你,
这很疯狂,
但这是我的电话号码(代表)
所以如果事情发生(事件),
打电话给我,也许(callback)?

callback是一个函数,当一个进程完成一个特定的任务时会被调用。

callback的用法通常是asynchronous逻辑。

要在C#中创build一个callback函数,你需要在一个variables中存储一个函数地址。 这是使用delegate或新的lambda语义FuncAction

  public delegate void WorkCompletedCallBack(string result); public void DoWork(WorkCompletedCallBack callback) { callback("Hello world"); } public void Test() { WorkCompletedCallBack callback = TestCallBack; // Notice that I am referencing a method without its parameter DoWork(callback); } public void TestCallBack(string result) { Console.WriteLine(result); } 

在今天的C#中,这可以使用lambda来完成,如:

  public void DoWork(Action<string> callback) { callback("Hello world"); } public void Test() { DoWork((result) => Console.WriteLine(result)); } 

定义

callback是作为parameter passing给其他代码的可执行代码。

履行

 // Parent can Read public class Parent { public string Read(){ /*reads here*/ }; } // Child need Info public class Child { private string information; // declare a Delegate delegate string GetInfo(); // use an instance of the declared Delegate public GetInfo GetMeInformation; public void ObtainInfo() { // Child will use the Parent capabilities via the Delegate information = GetMeInformation(); } } 

用法

 Parent Peter = new Parent(); Child Johny = new Child(); // Tell Johny from where to obtain info Johny.GetMeInformation = Peter.Read; Johny.ObtainInfo(); // here Johny 'asks' Peter to read 

链接

  • 更多 C#的细节 。

callback是一个函数指针,你传递给另一个函数。 您正在调用的函数将在完成时“callback”(执行)另一个函数。

看看这个链接。

如果您提到ASP.Netcallback:

在ASP.NET Web页面的默认模型中,用户与页面进行交互,然后单击button或执行一些其他操作,导致回发。 页面及其控件被重新创build,页面代码在服务器上运行,并且新版本的页面被呈现给浏览器。 但是,在某些情况下,从客户端运行服务器代码而不执行回发是非常有用的。 如果页面中的客户端脚本正在维护某些状态信息(例如,本地variables值),则发布该页面并获取其新副本会破坏该状态。 此外,页面回发引入了处理开销,可能会降低性能并强制用户等待页面被处理和重新创build。

为了避免丢失客户端状态并且不会导致服务器往返处理开销,可以编写一个ASP.NET Web页面,以便它可以执行客户端callback。 在客户端callback中,客户端脚本函数将请求发送到ASP.NET网页。 网页运行正常生命周期的修改版本。 页面被启动,其控件和其他成员被创build,然后调用一个特别标记的方法。 该方法执行已编码的处理,然后向浏览器返回一个值,该值可以被另一个客户端脚本函数读取。 在整个过程中,该页面在浏览器中生效。

资料来源: http : //msdn.microsoft.com/en-us/library/ms178208.aspx

如果您在代码中提到callback:

callback通常是在特定操作完成或执行子操作时调用的方法的委托。 你会经常在asynchronous操作中find它们。 这是几乎所有编码语言都可以find的编程原则。

更多信息在这里: http : //msdn.microsoft.com/en-us/library/ms173172.aspx

可能不是字典定义,但callback通常是指一个函数,它是一个特定对象的外部,存储,然后调用一个特定的事件。

一个例子可能是当UIbutton被创build时,它存储对执行动作的函数的引用。 该操作由代码的不同部分处理,但按下button时,将调用callback,并调用要执行的操作。

C#,而不是使用术语“callback”使用“事件”和“代表”,你可以在这里find更多关于代表。

callback函数允许您将可执行代码作为parameter passing给其他代码。 在C和C ++中,这是作为一个函数指针来实现的。 在.NET中,您将使用委托来pipe理函数指针。

一些用途包括错误信号和控制function是否起作用。

维基百科

callback工作步骤:

1)我们必须实现ICallbackEventHandler接口

2)注册客户端脚本:

  String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript = "function UseCallBack(arg, context)" + "{ " + cbReference + ";}"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack", callbackScript, true); 

1)从UI调用Onclient点击调用javascript函数为EX: – builpopup(p1,p2,p3...)

var finalfield = p1,p2,p3; UseCallBack(finalfield, ""); 来自客户端的数据通过使用UseCallBack传递给服务器端

2) public void RaiseCallbackEvent(string eventArgument)在eventArgument中,我们获取传递的数据//做一些服务器端操作,并传递给“callbackResult”

3) GetCallbackResult() //使用这个方法将数据传递给客户端(ReceiveServerData()函数)一侧

callbackResult

4)获取客户端的数据: ReceiveServerData(text) ,在文本服务器响应,我们会得到。

 Dedication to LightStriker: Sample Code: class CallBackExample { public delegate void MyNumber(); public static void CallMeBack() { Console.WriteLine("He/She is calling you. Pick your phone!:)"); Console.Read(); } public static void MetYourCrush(MyNumber number) { int j; Console.WriteLine("is she/he interested 0/1?:"); var i = Console.ReadLine(); if (int.TryParse(i, out j)) { var interested = (j == 0) ? false : true; if (interested)//event { //call his/her number number(); } else { Console.WriteLine("Nothing happened! :("); Console.Read(); } } } static void Main(string[] args) { MyNumber number = Program.CallMeBack; Console.WriteLine("You have just met your crush and given your number"); MetYourCrush(number); Console.Read(); Console.Read(); } } Code Explanation: I created the code to implement the funny explanation provided by LightStriker in the above one of the replies. We are passing delegate (number) to a method (MetYourCrush). If the Interested (event) occurs in the method (MetYourCrush) then it will call the delegate (number) which was holding the reference of CallMeBack method. So, the CallMeBack method will be called. Basically, we are passing delegate to call the callback method. Please let me know if you have any questions.