C # base - Callback Callback

This was written in 2011, a technical paste, now it may be a bit out of date, interested friends can look at.

I. Description

When developing winform program does not consider page refresh problem, as long as control of threads do not result in suspended animation ok, but in the development of web page program refresh sometimes it is really a headache. Page callback can solve this problem, the following simple practical terms to do the next introduction to callback.

The process can simply be understood:

The client  by fun_callbk ()  The value  passed to  the service side of the RaiseCallbackEvent () ;

The server has been dealt with by  () GetCallbackResult  the value and  return to  the client 's  showMsg () 

II. To explain

Internet seen a lot of it on to explain what the principle is confused at first, if only to use, do not know so much, those principles when developing web network features can better understand what relevant knowledge can be found online at here I will briefly talk about how to use.

In fact, as long as you finish the five steps to complete, it is very simple:

 

1. Set a trigger event, such as:

<div οnclick = "fun_callbk ( 'Hello everyone I am Wang Dong Silver)"> the Click Me </ div>

 

2. Write two client functions, such as:

...

function fun_callbk (msg) // This function method is that you click on the event to be triggered, which would write a sentence

{

<%=this.ClientScript.GetCallbackEventReference(this,"msg","showMsg",null)%>

// This sentence simply, it is registered in a reference to the client, where the parameters you can go to a Web search, general enough to use four parameters

// The first argument: refers to the page class

// The second parameter: Parameter event-triggered method

// third parameter: the server returns the receiver function data, will be defined below

// The fourth parameter: it is usually set to null

}

 

function showMsg (rValue) // this is the top of the third argument, accept the values returned from the server

{

alert (rValue); // write simple point, everyone would get so complicated dazzled

}

 

3. Write the server program

This step is relatively simple, you must do three things:

. A reference to an interface: ICallbackEventHandler, it is necessary, and in front of the class or interface to, spaced apart (this is the basis for the c #)

. B declare a first function, for receiving the value sent by the client, as follows:

// Remember: function name can not be casual , must be before they can RaiseCallbackEvent

// first declare a variable, easy to explain

string myStr=string.Empty;

public void RaiseCallbackEvent(string arg)

{

myStr = arg + "hello"; // or simply write the value assigned to the client we came newly declared variable

// add a hello, in order to verify that the data is indeed returned by the server process after

}

c. statement another function, the result returned to the client for

// this function name must also be GetCallbackResult, can not be modified

String public the GetCallbackResult ()     {return myStr; // returns the result to the client}

 

Well, here you have basically done!

III. To do some summary

In fact, it used four methods, fun_callbk (), showMsg (), GetCallbackResult (), RaiseCallbackEvent ()

Need to add a reference: <% = this.ClientScript.GetCallbackEventReference (this, "arg," showMsg ", null)%>

Needs to inherit an interface: ICallbackEventHandler these 

 

Case II: Implementing callbacks by way of commission

public class Lib
{
        public delegate void UserFunctionCB();
        private static UserFunctionCB m_userFnCB;

        /// <summary>
        /// 初始化时把需要回调的函数传入
        /// </summary>
        /// <param name="func"></param>
        public static void InitCallbackFunction(UserFunctionCB func)
        {
            m_userFnCB = func;
        }

        /// <summary>
        /// 当程序出现异常时会自动触发此函数
        /// (在这里我就不过多的描述异常捕获的过程了)
        /// </summary>
        /// <param name="func"></param>
        public static void HandleExceptionForNDK()
        {
            // 出现异常时调用用户自定义函数
            CallUserFunction();
        }

        /// <summary>
        /// 回调执行用户设置的函数
        /// </summary>
        private static void CallUserFunction()
        {
            if (m_userFnCB != null)
            {
                IAsyncResult result = m_userFnCB.BeginInvoke(delegate(IAsyncResult ar)
                {
                    m_userFnCB.EndInvoke(ar);
                }, null);

                // 执行50毫秒后超时
                result.AsyncWaitHandle.WaitOne(50, true);
            }
        }
}

The following is a partial call:

public class Demo
{
    void Start()
    {
        var m_callbackFn = new Lib.UserFunctionCB(CallBackFunction);
        Lib.InitCallbackFunction(m_callbackFn);
    }

    /// <summary>
    /// 回调函数实现部份
    /// </summary>
    private void CallBackFunction()
    {
         Debug.LogError("print of call back function!");
    }
}

 Since this example is used in a Unity, pull out the code directly, not through a direct copy of a compiled.

 

 

Published 85 original articles · won praise 30 · views 270 000 +

Guess you like

Origin blog.csdn.net/qq_42672770/article/details/104424573