123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
-
- using System;
- using System.Diagnostics;
- using System.Threading;
- using Aliyun.OSS.Common.Communication;
- namespace Aliyun.OSS.Util
- {
-
-
-
-
- internal abstract class AsyncResult : IAsyncResult, IDisposable
- {
- #region Fields
- private readonly object _asyncState;
- private bool _isCompleted;
- private readonly AsyncCallback _userCallback;
- private ManualResetEvent _asyncWaitEvent;
- private Exception _exception;
- #endregion
- #region IAsyncResult Members
-
-
-
- public object AsyncState
- {
- get { return _asyncState; }
- }
- public AsyncCallback Callback
- {
- get { return _userCallback; }
- }
-
-
-
- public WaitHandle AsyncWaitHandle
- {
- get
- {
- if (_asyncWaitEvent != null)
- return _asyncWaitEvent;
- _asyncWaitEvent = new ManualResetEvent(false);
- if (IsCompleted)
- _asyncWaitEvent.Set();
- return _asyncWaitEvent;
- }
- }
-
-
-
- public bool CompletedSynchronously { get; protected set; }
-
-
-
- [DebuggerNonUserCode]
- public bool IsCompleted
- {
- get { return _isCompleted; }
- }
- #endregion
-
-
-
-
-
- protected AsyncResult(AsyncCallback callback, object state)
- {
- _userCallback = callback;
- _asyncState = state;
- }
-
-
-
-
- public void Complete(Exception ex)
- {
- _exception = ex;
- NotifyCompletion();
- }
-
-
-
-
- protected void WaitForCompletion()
- {
- if (!IsCompleted)
- AsyncWaitHandle.WaitOne();
- if (_exception != null)
- throw _exception;
- }
-
-
-
-
- protected void NotifyCompletion()
- {
- _isCompleted = true;
- if (_asyncWaitEvent != null)
- _asyncWaitEvent.Set();
- if (_userCallback != null)
- {
- var httpAsyncResult = this as ServiceClientImpl.HttpAsyncResult;
- Debug.Assert(httpAsyncResult != null);
- _userCallback(httpAsyncResult.AsyncState as RetryableAsyncResult);
- }
- }
- #region IDisposable Members
-
-
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
-
-
-
- protected virtual void Dispose(bool disposing)
- {
- if (disposing && _asyncWaitEvent != null)
- {
- _asyncWaitEvent.Close();
- _asyncWaitEvent = null;
- }
- }
- #endregion
- }
-
-
-
-
-
- internal class AsyncResult<T> : AsyncResult
- {
-
-
-
- private T _result;
-
-
-
-
-
- public AsyncResult(AsyncCallback callback, object state)
- : base(callback, state)
- { }
-
-
-
-
- public T GetResult()
- {
- base.WaitForCompletion();
- return _result;
- }
-
-
-
-
- public void Complete(T result)
- {
-
- _result = result;
- base.NotifyCompletion();
- }
- }
- }
|