using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Leaf.Parallel
{
internal class NDefaultStrategy : IParallelStrategy
{
NExceptionDel exceptionRoutine = null;
public NDefaultStrategy()
{
}
#region IMPL of the parallel invocation
NResult<T> ExecuteImpl<T>(NFunc<T> asyncRoutine, NResultDel<T> callbackRoutine, NExceptionDel exceptionRoutine, CallbackMode callbackMode)
{
NResultDefault<NFunc<T>, NResultDel<T>, T> result =
new NResultDefault<NFunc<T>, NResultDel<T>, T>(asyncRoutine, callbackRoutine, exceptionRoutine);
NQueue.Instance.EnQueue(result);
IAsyncResult ar = asyncRoutine.BeginInvoke(result.AutoCallback, null);
result.CallbackMode = callbackMode;
result.AsyncHandle = ar;
Debug.Assert(ar != null);
return result;
}
NResult<NVoid> ExecuteImpl(NVoidCallDel asyncRoutine, NVoidCallDel callback, NExceptionDel exceptionRoutine, CallbackMode marshal)
{
NResultDefault<NVoidCallDel, NVoidCallDel, NVoid> result =
new NResultDefault<NVoidCallDel, NVoidCallDel, NVoid>(asyncRoutine, callback, exceptionRoutine);
result.CallbackMode = marshal;
NQueue.Instance.EnQueue(result);
IAsyncResult ar = asyncRoutine.BeginInvoke(result.AutoCallback, null);
result.AsyncHandle = ar;
Debug.Assert(ar != null && result != null);
return result;
}
#endregion
#region IParallelStrategy Members
public NResult<T> Execute<T>(NFunc<T> asyncRoutine, NResultDel<T> callbackRoutine, NExceptionDel exceptionRoutine, CallbackMode callbackMode)
{
if (exceptionRoutine == null)
{
exceptionRoutine = this.exceptionRoutine;
}
return ExecuteImpl<T>(asyncRoutine, callbackRoutine,exceptionRoutine, callbackMode);
}
public NResult<NVoid> Execute(NVoidCallDel asyncRoutine, NVoidCallDel callbackRoutine, NExceptionDel exceptionRoutine, CallbackMode callbackMode)
{
if (exceptionRoutine == null)
{
exceptionRoutine = this.exceptionRoutine;
}
return ExecuteImpl(asyncRoutine, callbackRoutine, exceptionRoutine, callbackMode);
}
public NExceptionDel ExceptionDel {
get
{
return exceptionRoutine;
}
set
{
exceptionRoutine = value;
}
}
#endregion
void DumpException(Exception exc)
{
Console.WriteLine("Exception : {0}" , exc.Message);
Console.WriteLine("CallStack : {0}", exc.StackTrace);
}
}
}