Windows Phone 7 异步编程模型

Windows Phone 7上的异步编程模型其实也就是说把C#里面的异步编程模型在Windows Phone 7应用开发上使用。下面来看一下异步编程模型里面的一些关键的概念。

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;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="asyncCallback">异步返回方法</param>
        /// <param name="state">异步调用对象</param>
        public AsyncResultNoResult(AsyncCallback asyncCallback, object state)
        {
            this._asyncCallback = asyncCallback;
            this._asyncState = state;
        }
        /// <summary>
        /// 结束任务
        /// </summary>
        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);//调用异步请求的返回方法
            }
        }
        /// <summary>
        /// 放弃异步请求的对象
        /// </summary>
        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<ManualResetEvent>(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<TResult> : AsyncResultNoResult
    {
        private TResult m_result;//异步操作完成返回的对象

        public AsyncResult(AsyncCallback asyncCallback, object state)
            : base(asyncCallback, state)
        {
            this.m_result = default(TResult);
        }
        /// <summary>
        /// 停止异步返回返回对象
        /// </summary>
        /// <returns></returns>
        public TResult EndInvoke()
        {
            base.EndInvoke(); //等待操作完成 
            return m_result;  // 返回结果
        }
        /// <summary>
        /// 操作完成
        /// </summary>
        /// <param name="result">返回结果对象</param>
        /// <param name="completedSynchronously">是否同步操作</param>
        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<string> asyncResult = new AsyncResult<string>(asyncCallback, state);
            ThreadPool.QueueUserWorkItem(DoTestTask, asyncResult);
            return asyncResult;
        }

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

        private void DoTestTask(Object asyncResult)
        {
            AsyncResult<string> ar = (AsyncResult<string>)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<string, IObservable<string>> testResult = Observable.FromAsyncPattern<string, string>(tt.BeginTestTask, tt.EndTestTask);

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

运行的效果

posted on 2012-06-09 16:32  linzheng  阅读(1708)  评论(3编辑  收藏  举报

导航