zoukankan      html  css  js  c++  java
  • AsyncResult 类的使用

    AsyncResult 类封装异步委托上的异步操作的结果,与异步委托一起使用。从该委托的 BeginInvoke 方法返回的 IAsyncResult 可以强制转换为 AsyncResult。AsyncResult 具有 AsyncDelegate 属性,该属性保存在其上调用异步调用的委托对象。

    AsyncResult 类的属性

    AsyncDelegate 获取在其上调用异步调用的委托对象。
    Public property AsyncState获取作为 BeginInvoke 方法调用的最后一个参数而提供的对象。
    Public property AsyncWaitHandle获取封装 Win32 同步句柄并允许实现各种同步方案的 WaitHandle。
    Public property CompletedSynchronously获取一个值,该值指示 BeginInvoke 调用是否同步完成。
    Public property EndInvokeCalled获取或设置一个值,该值指示是否已在当前 AsyncResult 上调用 EndInvoke。
    Public property IsCompleted获取一个值,该值指示服务器是否已完成该调用。

    Public property NextSink 获取接收器链中的下一个消息接收器。


    下面的代码示例说明如何使用 AsyncResult 类检索异步委托上的异步操作的结果。

    using System;
    using System.Threading;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Contexts;
    using System.Runtime.Remoting.Messaging;
    
    // Context-bound type with the Synchronization context attribute.
    [Synchronization()]
    public class SampleSyncronized : ContextBoundObject {
    
        // A method that does some work, and returns the square of the given number.
        public int Square(int i)  {
    
            Console.Write("The hash of the thread executing ");
            Console.WriteLine("SampleSyncronized.Square is: {0}", 
                                 Thread.CurrentThread.GetHashCode());
            return i*i;
        }
    }
    
    // The async delegate used to call a method with this signature asynchronously.
    public delegate int SampSyncSqrDelegate(int i);
    
    public class AsyncResultSample {
    
        // Asynchronous Callback method.
        public static void MyCallback(IAsyncResult ar) {
    
            // Obtains the last parameter of the delegate call.
            int value = Convert.ToInt32(ar.AsyncState);
    
            // Obtains return value from the delegate call using EndInvoke.
            AsyncResult aResult = (AsyncResult)ar;
            SampSyncSqrDelegate temp = (SampSyncSqrDelegate)aResult.AsyncDelegate;
            int result = temp.EndInvoke(ar);
    
            Console.Write("Simple.SomeMethod (AsyncCallback): Result of ");
            Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result);
        }
    
        public static void Main() {
    
            int result;
            int param;
    
            // Creates an instance of a context-bound type SampleSynchronized.
            SampleSyncronized sampSyncObj = new SampleSyncronized();
    
            // Checks whether the object is a proxy, since it is context-bound.
            if (RemotingServices.IsTransparentProxy(sampSyncObj))
                Console.WriteLine("sampSyncObj is a proxy.");
            else
                Console.WriteLine("sampSyncObj is NOT a proxy.");
    
            param = 10;
    
            Console.WriteLine("");
            Console.WriteLine("Making a synchronous call on the context-bound object:");
    
            result = sampSyncObj.Square(param);
            Console.Write("The result of calling sampSyncObj.Square with ");
            Console.WriteLine("{0} is {1}.", param, result);
            Console.WriteLine("");
    
            SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square);
            param = 8;
    
            Console.WriteLine("Making a single asynchronous call on the context-bound object:");
    
            IAsyncResult ar1 = sampleDelegate.BeginInvoke( param, 
                                  new AsyncCallback(AsyncResultSample.MyCallback), 
                                  param);
    
            Console.WriteLine("Waiting for the asynchronous call to complete...");
            WaitHandle wh = ar1.AsyncWaitHandle;
            wh.WaitOne();
    
            Console.WriteLine("");
            Console.WriteLine("Waiting for the AsyncCallback to complete...");
            Thread.Sleep(1000);
        }
    }





    版权声明:

  • 相关阅读:
    NOIP2016 天天爱跑步 正解
    NOIP2016 换教室
    iOS开发-14款状态栏(StatusBar)开源软件
    iOS 优化方案浅析
    iOS应用程序多语言本地化解决方案
    iOS开发流程总结
    iOS开发者必备:四款后端服务工具
    iOS开发之加载、滑动翻阅大量图片优化解决方案
    iOS开发之──传感器使用
    iOS开发中的Html解析方法
  • 原文地址:https://www.cnblogs.com/walccott/p/4957069.html
Copyright © 2011-2022 走看看