Public classlib{ Public Delegate voidUSERFUNCTIONCB (); Private StaticUSERFUNCTIONCB M_USERFNCB; /// <summary> ///The function that needs the callback is passed in at initialization/// </summary> /// <param name= "func" ></param> Public Static voidinitcallbackfunction (USERFUNCTIONCB func) {M_USERFNCB=func; } /// <summary> ///This function is automatically triggered when an exception occurs in the program///(I don't have to describe the process of exception capture too much here)/// </summary> /// <param name= "func" ></param> Public Static voidhandleexceptionforndk () {//invoke user-defined function when an exception occurscalluserfunction (); } /// <summary> ///callback function to execute user settings/// </summary> Private Static voidcalluserfunction () {if(M_USERFNCB! =NULL) {IAsyncResult result= M_userfncb.begininvoke (Delegate(IAsyncResult ar) {m_userfncb.endinvoke (AR); }, NULL); //timeout After 50 milliseconds of executionResult. Asyncwaithandle.waitone ( -,true); } }}
The following is the invocation part:
Public classdemo{voidStart () {varM_CALLBACKFN =NewLIB.USERFUNCTIONCB (callbackfunction); Lib.initcallbackfunction (M_CALLBACKFN); } /// <summary> ///callback function Implementation part/// </summary> Private voidcallbackfunction () {Debug.logerror ("print of call back function!"); }}
The above is to implement the callback operation by the way of the delegate! Since this example is used under unity, the direct copy of the code is not compiled.
Just write here at the beginning of a notepad to avoid forgetting.
C # Implementation Function callback