简单实现C#异步操作

2019-12-26 17:28:50王旭

无返回异步操作
ActionAsync


public class ActionAsync : Operator
{
  private readonly Action _action;
  protected ActionAsync()
  {
  }
  public ActionAsync(Action action)
    : this()
  {
    this._action = action;
  }
  public override IAsyncResult Invoke()
  {
    var middle = _action.BeginInvoke(CompletedCallBack, null);
    SetAsyncResult(middle);
    return middle;
  }
  public override void CompletedCallBack(IAsyncResult ar)
  {
    try
    {
      _action.EndInvoke(ar);
    }
    catch (Exception exception)
    {
      this.CatchException(exception);
    }
    ContinueAsync();
  }
}
public class ActionAsync<T> : ActionAsync
{
  public T Result;
  private readonly Action<T> _action1;
  protected readonly T Parameter1;
  public ActionAsync()
  {
  }
  public ActionAsync(T parameter)
  {
    this.Parameter1 = parameter;
  }
  public ActionAsync(Action<T> action, T parameter)
  {
    this._action1 = action;
    this.Parameter1 = parameter;
  }
  public override IAsyncResult Invoke()
  {
    var result = _action1.BeginInvoke(Parameter1, CompletedCallBack, null);
    SetAsyncResult(result);
    return result;
  }
  public override void CompletedCallBack(IAsyncResult ar)
  {
    try
    {
      _action1.EndInvoke(ar);
    }
    catch (Exception exception)
    {
      this.CatchException(exception);
    }
    ContinueAsync();
  }
}

有返回异步
FuncAsync实现了IFuncOperationAsync接口

IFuncOperationAsync


public interface IFuncOperationAsync<T>
{
  void SetResult(T result);
  T GetResult();
}
  • SetResult(T result):异步操作完成设置返回值
  • GetResult():获取返回值

    1)、FuncAsync

    
    public class FuncAsync<TResult> : Operator, IFuncOperationAsync<TResult>
    {
    private TResult _result;
    
    public TResult Result
    {
      get
      {
        if (!Middle.IsCompleted || _result == null)
        {
          _result = GetResult();
        }
        return _result;
      }
    }
    private readonly Func<TResult> _func1;
    public FuncAsync()
    {
    }
    public FuncAsync(Func<TResult> func)
    {
      this._func1 = func;
    }
    public override IAsyncResult Invoke()
    {
      var result = _func1.BeginInvoke(CompletedCallBack, null);
      SetAsyncResult(result);
      return result;
    }
    public override void CompletedCallBack(IAsyncResult ar)
    {
      try
      {
        var result = _func1.EndInvoke(ar);
        SetResult(result);
      }
      catch (Exception exception)
      {
        this.CatchException(exception);
        SetResult(default(TResult));
      }
      ContinueAsync();
    }
    public virtual TResult GetResult()
    {
      Wait();
      return this._result;
    }
    public void SetResult(TResult result)
    {
      _result = result;
    }
    }
    public class FuncAsync<T1, TResult> : FuncAsync<TResult>
    {
    protected readonly T1 Parameter1;
    private readonly Func<T1, TResult> _func2;
    public FuncAsync(Func<T1, TResult> action, T1 parameter1)
      : this(parameter1)
    {
      this._func2 = action;
    }
    protected FuncAsync(T1 parameter1)
      : base()
    {
      this.Parameter1 = parameter1;
    }
    public override IAsyncResult Invoke()
    {
      var result = _func2.BeginInvoke(Parameter1, CompletedCallBack, null);
      SetAsyncResult(result);
      return result;
    }
    public override void CompletedCallBack(IAsyncResult ar)
    {
      try
      {
        var result = _func2.EndInvoke(ar);
        SetResult(result);
      }
      catch (Exception exception)
      {
        CatchException(exception);
        SetResult(default(TResult));
      }
      ContinueAsync();
    }
    }