zoukankan      html  css  js  c++  java
  • 異步調用

    我們知道可以用control.beginInvoke() 和 control.EndInvoke()來進行異步調用,但endInvoke()是block的,如果在調用者函數體內執行還是達不到真正的異步,今天在網上看到一篇可以做到真正異步,在調用都函數體內調用control.BeginInvoke(),然后傳遞一個callback函數指針,在被調用者函數體內調用EndInvoke()并激活調用者傳進來的callback,以便通知調用者執行結果
    Linkage為http://www.codeproject.com/csharp/AsyncMethodInvocation.asp

    <PRE lang=cs id=pre19 style="MARGIN-TOP: 0px">private void CallFooWithOutAndRefParametersWithCallback()
    {
        // create the paramets to pass to the function
        string strParam1 = "Param1";
        int intValue = 100;
        ArrayList list = new ArrayList();
        list.Add("Item1");

        // create the delegate
        DelegateWithOutAndRefParameters delFoo =
            new DelegateWithOutAndRefParameters(FooWithOutAndRefParameters);

        delFoo.BeginInvoke(strParam1,
            out intValue,
            ref list,
            new AsyncCallback(CallBack), // callback delegate!
            null);
    }

    private void CallBack(IAsyncResult ar)
    {
        // define the output parameter
        int intOutputValue;
        ArrayList list = null;

        // first case IAsyncResult to an AsyncResult object, so we can get the
        // delegate that was used to call the function.
        AsyncResult result = (AsyncResult)ar;

        // grab the delegate
        DelegateWithOutAndRefParameters del =
            (DelegateWithOutAndRefParameters) result.AsyncDelegate;

        // now that we have the delegate,
        // we must call EndInvoke on it, so we can get all
        // the information about our method call.

        string strReturnValue = del.EndInvoke(out intOutputValue,
            ref list, ar);
    }</PRE>

  • 相关阅读:
    集大软件工程15级结对编程week1
    集大软件工程15级个人作业Week2
    集大软件工程15级个人作业Week1
    Java 课程设计 "Give it up"小游戏(团队)
    Java 课程设计 "Give it up"小游戏设计(个人) 201521123077
    201521123077 《Java程序设计》第14周学习总结
    201521123077 《Java程序设计》第13周学习总结
    2019-助教总结
    软工的总分
    第四次团队作业
  • 原文地址:https://www.cnblogs.com/sdikerdong/p/944602.html
Copyright © 2011-2022 走看看