2025-05-12 05:00:00

什么是回调?

什么是回调,它在c#中是如何实现的?


当前回答

如果您指的是ASP。净回调函数:

In the default model for ASP.NET Web pages, the user interacts with a page and clicks a button or performs some other action that results in a postback. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser. However, in some situations, it is useful to run server code from the client without performing a postback. If the client script in the page is maintaining some state information (for example, local variable values), posting the page and getting a new copy of it destroys that state. Additionally, page postbacks introduce processing overhead that can decrease performance and force the user to wait for the page to be processed and re-created. To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.

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

如果你在代码中引用回调:

回调通常是对方法的委托,在特定操作完成或执行子操作时调用这些方法。您将经常在异步操作中发现它们。这是一种在几乎所有编码语言中都可以找到的编程原则。

更多信息请访问:http://msdn.microsoft.com/en-us/library/ms173172.aspx

其他回答

如果您指的是ASP。净回调函数:

In the default model for ASP.NET Web pages, the user interacts with a page and clicks a button or performs some other action that results in a postback. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser. However, in some situations, it is useful to run server code from the client without performing a postback. If the client script in the page is maintaining some state information (for example, local variable values), posting the page and getting a new copy of it destroys that state. Additionally, page postbacks introduce processing overhead that can decrease performance and force the user to wait for the page to be processed and re-created. To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.

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

如果你在代码中引用回调:

回调通常是对方法的委托,在特定操作完成或执行子操作时调用这些方法。您将经常在异步操作中发现它们。这是一种在几乎所有编码语言中都可以找到的编程原则。

更多信息请访问:http://msdn.microsoft.com/en-us/library/ms173172.aspx

回调工作步骤:

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调用在客户端点击调用javascript函数为EX:- builpopup(p1,p2,p3…)

Var finalfield= p1,p2,p3; UseCallBack (finalfield”、“);使用UseCallBack将客户端的数据传递给服务器端

在eventArgument中我们获取传递的数据 //执行一些服务器端操作并传递给"callbackResult"

3) GetCallbackResult() //使用此方法数据将被传递到客户端(ReceiveServerData()函数)端

callbackResult

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

我才刚认识你, 这很疯狂, 这是我的number (delegate) 所以如果发生了什么(事件), 打给我吧(回电话)?

定义

回调是可执行的代码 作为参数传递给其他代码。

实现

// 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#的更多细节。

回调函数是在流程执行完特定任务时调用的函数。

回调的使用通常是在异步逻辑中。

要在c#中创建回调,需要在变量中存储函数地址。这是使用委托或新的lambda语义Func或Action实现的。

    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));
        DoWork(Console.WriteLine); // This also works
    }