异步编程:异步编程模型(APM)
作者:网络转载 发布时间:[ 2013/4/3 9:49:50 ] 推荐标签:
一、基于IAsyncResult构造一个异步API
现在来构建一个IAsyncResult的类,并且实现异步调用。
// 带ref参数的自定义委托
public delegate void RefAsyncCallback(ref string resultStr, IAsyncResult ar);
public class CalculateAsyncResult : IAsyncResult
{
private int _calcNum1;
private int _calcNum2;
private RefAsyncCallback _userCallback;
public CalculateAsyncResult(int num1, int num2, RefAsyncCallback userCallback, object asyncState)
{
this._calcNum1 = num1;
this._calcNum2 = num2;
this._userCallback = userCallback;
this._asyncState = asyncState;
// 异步执行操作
ThreadPool.QueueUserWorkItem((obj) => { AsyncCalculate(obj); }, this);
}
#region IAsyncResult接口
private object _asyncState;
public object AsyncState { get { return _asyncState; } }
private ManualResetEvent _asyncWaitHandle;
public WaitHandle AsyncWaitHandle
{
get
{
if (this._asyncWaitHandle == null)
{
ManualResetEvent event2 = new ManualResetEvent(false);
Interlocked.CompareExchange<ManualResetEvent>(ref this._asyncWaitHandle, event2, null);
}
return _asyncWaitHandle;
}
}
private bool _completedSynchronously;
public bool CompletedSynchronously { get { return _completedSynchronously; } }
private bool _isCompleted;
public bool IsCompleted { get { return _isCompleted; } }
#endregion
/// <summary>
///
/// 存储后结果值
/// </summary>
public int FinnalyResult { get; set; }
/// <summary>
/// End方法只应调用一次,超过一次报错
/// </summary>
public int EndCallCount = 0;
/// <summary>
/// ref参数
/// </summary>
public string ResultStr;
/// <summary>
/// 异步进行耗时计算
/// </summary>
/// <param name="obj">CalculateAsyncResult实例本身</param>
private static void AsyncCalculate(object obj)
{
CalculateAsyncResult asyncResult = obj as CalculateAsyncResult;
Thread.SpinWait(1000);
asyncResult.FinnalyResult = asyncResult._calcNum1 * asyncResult._calcNum2;
asyncResult.ResultStr = asyncResult.FinnalyResult.ToString();
// 是否同步完成
asyncResult._completedSynchronously = false;
asyncResult._isCompleted = true;
((ManualResetEvent)asyncResult.AsyncWaitHandle).Set();
if (asyncResult._userCallback != null)
asyncResult._userCallback(ref asyncResult.ResultStr, asyncResult);
}
}
public class CalculateLib
{
public IAsyncResult BeginCalculate(int num1, int num2, RefAsyncCallback userCallback, object asyncState)
{
CalculateAsyncResult result = new CalculateAsyncResult(num1, num2, userCallback, asyncState);
return result;
}
public int EndCalculate(ref string resultStr, IAsyncResult ar)
{
CalculateAsyncResult result = ar as CalculateAsyncResult;
if (Interlocked.CompareExchange(ref result.EndCallCount, 1, 0) == 1)
{
throw new Exception("End方法只能调用一次。");
}
result.AsyncWaitHandle.WaitOne();
resultStr = result.ResultStr;
return result.FinnalyResult;
}
public int Calculate(int num1, int num2, ref string resultStr)
{
resultStr = (num1 * num2).ToString();
return num1 * num2;
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11