I need to start the list of methods synchronously, with the ability to stop the execution list. It's easy to stop the loop before executing with the reset event (see first line in Execute).
How can I wait for an answer from action.Execute()and action.Execute()at the same time?
private ManualResetEvent _abortingToken = new ManualResetEvent(false);
private List<IAction> _actions;
public void Abort()
{
_abortingToken.Set();
}
public void Execute()
{
foreach (var action in _actions)
{
if (_abortingToken.WaitOne(0))
break;
action.Execute();
while ()
{
if (_abortingToken.WaitOne(1))
action.Abort();
}
}
}
I think it would be easy to preform using Tasks, but unfortunately I am using .net 3.5.
Edit: Solution based on SLaks Responses :
public void Execute()
{
Action execute = null;
IAsyncResult result = null;
foreach (var action in _actions)
{
execute = new Action(scriptCommand.Execute);
if (_abortingToken.WaitOne(0))
break;
result = execute.BeginInvoke(null, null);
while (!result.IsCompleted)
{
if (_abortingToken.WaitOne(10))
{
action.Abort();
break;
}
}
execute.EndInvoke(result);
}
}
source
share