C # Implementation Function callback

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.