加菲猫 发表于 2013-1-4 01:17:52

Windows Phone 7 异步编程模型

Windows Phone 7 异步编程模型

<div id="cnblogs_post_body">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委托被作为开始操作上的第二个到最后一个参数传递
代码原型如下:

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
<div class="cnblogs_code">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);            }      }    }}
页: [1]
查看完整版本: Windows Phone 7 异步编程模型