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>

  • 相关阅读:
    How to change the property of a control from a flowlayoutpanel?
    Add controls dynamically in flowlayoutpanel
    CLR via C# 读书笔记 6-2 不同AppDomain之间的通信 z
    应用程序域 z
    C# 在SQLite数据库中存储图像 z
    Using .NET 4's Lazy<T> 实现单实例
    tpl demo
    SQLite批量插入,修改数据库 zt
    Dev表格导出工具类 z
    easyui 设置一加载,搜索框立即弹出的效果
  • 原文地址:https://www.cnblogs.com/sdikerdong/p/944602.html
Copyright © 2011-2022 走看看