Windows Phone 7 异步编程模型详解

作者:袖梨 2022-06-26

2个方法和一个委托和一个接口
(1)BeginInvoke方法用于启动异步调用
Begin 方法包含同步方法签名中的任何参数,此外还包含另外两个参数:一个AsyncCallback 委托和一个用户定义的状态对象。委托用来调用回调方法,状态对象是用来向回调方法传递状态信息。
BeginInvoke立即返回,不等待异步调用完成。
BeginInvoke返回IasyncResult,可用于监视调用进度。


(2)EndInvoke方法用于检索异步调用结果
End 方法用于结束异步操作并返回结果,因此包含同步方法签名中的 ref 和 out 参数,返回值类型也与同步方法相同。该方法还包括一个 IAsyncResult 参数,用于获取异步操作是否完成的信息,当然在使用时就必须传入对应的 Begin 方法返回的对象实例开始异步操作后如果要阻止应用程序,可以直接调用 End 方法,这会阻止应用程序直到异步操作完成后再继续执行。也可以使用 IAsyncResult 的 AsyncWaitHandle 属性,调用其中的WaitOne等方法来阻塞线程。这两种方法的区别不大,只是前者必须一直等待而后者可以设置等待超时。

如果不阻止应用程序,则可以通过轮循 IAsyncResult 的 IsCompleted 状态来判断操作是否完成,或使用 AsyncCallback 委托来结束异步操作。AsyncCallback 委托包含一个 IAsyncResult 的签名,回调方法内部再调用 End 方法来获取操作执行结果。

在调用BeginInvoke后可随时调用EndInvoke方法,注意:始终在异步调用完成后调用EndInvoke.如果异步调用未完成,EndInvoke将一直阻塞到异步调用完成。
EndInvoke的参数包括需要异步执行的方法的out和ref参数以及由BeginInvoke返回的IAsyncResult。要注意的是,始终在异步调用完成后调用EndInvoke

(3)AsyncCallback委托用于指定在开始操作完成后应被调用的方法
AsyncCallback委托被作为开始操作上的第二个到最后一个参数传递
代码原型如下:
[Serializable]

 代码如下 复制代码

public delegate void AsyncCallback(IAsyncResult ar);

(4)IAsyncResult接口
它表示异步操作的状态.
该接口定义了4个公用属性
public interface IAsyncResult
 { 
object AsyncState { get; }
 WaitHandle AsyncWaitHandle { get; }
  bool CompletedSynchronously { get; }
 bool IsCompleted { get; }
 }

 

Windows Phone 7上的应用

AsyncResultNoResult.cs

 代码如下 复制代码

using System;
using System.Threading;

namespace AsyncTaskDemo
{
    public class AsyncResultNoResult : IAsyncResult
    {
        private readonly AsyncCallback _asyncCallback;//异步请求的返回方法
        private readonly object _asyncState;//异步请求的对象

        private ManualResetEvent _asyncWaitHandle;//线程阻塞

        private Exception _exception;//操作异常

        private int _completedState;//完成状态
        private const int StateCompletedAsynchronously = 2;//异步完成
        private const int StateCompletedSynchronously = 1;//同步完成
        private const int StatePending = 0;
        ///


        /// 初始化
        ///

        /// 异步返回方法
        /// 异步调用对象
        public AsyncResultNoResult(AsyncCallback asyncCallback, object state)
        {
            this._asyncCallback = asyncCallback;
            this._asyncState = state;
        }
        ///
        /// 结束任务
        ///

        public void EndInvoke()
        {
            // This method assumes that only 1 thread calls EndInvoke
            // for this object
            if (!this.IsCompleted)//任务未完成
            {
                // If the operation isn't done, wait for it
                this.AsyncWaitHandle.WaitOne();
                this.AsyncWaitHandle.Close();
                this._asyncWaitHandle = null; // Allow early GC
            }
            if (this._exception != null)
            {
                RethrowException(this._exception);
            }
        }

        private static void RethrowException(Exception ex)
        {
            throw ex;
        }

        public void SetAsCompleted(Exception exception, bool completedSynchronously)
        {
            // Passing null for exception means no error occurred.
            // This is the common case
            this._exception = exception;
            // The m_CompletedState field MUST be set prior calling the callback
            if (Interlocked.Exchange(ref this._completedState, completedSynchronously ? 1 : 2) != 0)
            {
               
                throw new InvalidOperationException("You can set a result only once", exception);
            }
            if (this._asyncWaitHandle != null)
            {// If the event exists, set it
                this._asyncWaitHandle.Set();
            }
            if (this._asyncCallback != null)
            {// If a callback method was set, call it
                this._asyncCallback(this);//调用异步请求的返回方法
            }
        }
        ///


        /// 放弃异步请求的对象
        ///

        public object AsyncState
        {
            get
            {
                return this._asyncState;
            }
        }

        public WaitHandle AsyncWaitHandle
        {
            get
            {
               
                if (this._asyncWaitHandle == null)
                {
                    bool isCompleted = this.IsCompleted;
                    ManualResetEvent event2 = new ManualResetEvent(isCompleted);
                    if (Interlocked.CompareExchange(ref this._asyncWaitHandle, event2, null) != null)
                    {
                        // Another thread created this object's event; dispose
                        // the event we just created
                        event2.Close();
                    }
                    else if (!isCompleted && this.IsCompleted)
                    {
                        // If the operation wasn't done when we created
                        // the event but now it is done, set the event
                        //如果操作已经完成则释放阻塞
                        this._asyncWaitHandle.Set();
                    }
                }
                return this._asyncWaitHandle;
            }
        }
        //同步操作是否已经完成
        public bool CompletedSynchronously
        {
            get
            {
                return (this._completedState == 1);
            }
        }
        //是否已经完成
        public bool IsCompleted
        {
            get
            {
                return (this._completedState != 0);
            }
        }
    }
}

AsyncResult.cs

 代码如下 复制代码

using System;

namespace AsyncTaskDemo
{
    public class AsyncResult : AsyncResultNoResult
    {
        private TResult m_result;//异步操作完成返回的对象

        public AsyncResult(AsyncCallback asyncCallback, object state)
            : base(asyncCallback, state)
        {
            this.m_result = default(TResult);
        }
        ///


        /// 停止异步返回返回对象
        ///

        ///
        public TResult EndInvoke()
        {
            base.EndInvoke(); //等待操作完成
            return m_result;  // 返回结果
        }
        ///
        /// 操作完成
        ///

        /// 返回结果对象
        /// 是否同步操作
        public void SetAsCompleted(TResult result, bool completedSynchronously)
        {
            //保存异步操作的结果
            m_result = result;
            base.SetAsCompleted(null, completedSynchronously);
        }
    }
}

TestTask.cs

 代码如下 复制代码

using System;
using System.Threading;

namespace AsyncTaskDemo
{
    public class TestTask
    {
        public TestTask()
        {
        }

        public IAsyncResult BeginTestTask(string text, AsyncCallback asyncCallback, object state)
        {
            AsyncResult asyncResult = new AsyncResult(asyncCallback, state);
            ThreadPool.QueueUserWorkItem(DoTestTask, asyncResult);
            return asyncResult;
        }

        public string EndTestTask(IAsyncResult asyncResult)
        {
            return ((AsyncResult)asyncResult).EndInvoke();
        }

        private void DoTestTask(Object asyncResult)
        {
            AsyncResult ar = (AsyncResult)asyncResult;
            try
            {
                Thread.Sleep(3000);
                ar.SetAsCompleted("测试完成", true);
            }
            catch (Exception e)
            {
                ar.SetAsCompleted(e, false);
            }
        }
    }
}

MainPage.xaml.cs

 代码如下 复制代码

TestTask tt = new TestTask();
        WebTask webTask = new WebTask(new Uri("http://www.cnblogs.com/linzheng"));
        IAsyncResult ia;
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            Func> testResult = Observable.FromAsyncPattern(tt.BeginTestTask, tt.EndTestTask);

            ObservableExtensions.Subscribe(
               Observable.ObserveOnDispatcher
               (
               testResult.Invoke("")
             ),
              delegate(string success)
              {
                  MessageBox.Show(success);
              },
              delegate(Exception exception)
              {
                  MessageBox.Show(exception.Message);
              }
              );
        }

运行的效果

相关文章

精彩推荐