I am trying to create a convenient extension method for Action to basically start this action after a delay: while my extension looks like this:
public static void DelayAction(this Action DelayedAction, int millisecondDelay, CancellationToken Token) { Task t = Task.Factory.StartNew(() => { Thread.Sleep(millisecondDelay); }, Token, TaskCreationOptions.None, TaskScheduler.Default); t.ContinueWith(_ => DelayedAction, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); }
I have a function that I call, which in turn uses these extensions
private void DelayTask(Action ActiontoDelay, int millisecondDelay) { ActiontoDelay.DelayAction(millisecondDelay, _TokenSource.Token); }
What I call so:
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
But all this seems to throw the whole, and the action never works. Where am I mistaken?
Edit 17-11-11 2300 hours:
I removed the general extension method, since it is not relevant to this example.
Also posting a comment here, as it does not format the code explicitly in the comments
If instead of calling
DelayTask(() => { _SomeFunction(SomeArgs); }, 1500);
I do it directly:
Task t = Task.Factory.StartNew(() => { Thread.Sleep(1500); }, Token, TaskCreationOptions.None, TaskScheduler.Default); t.ContinueWith(() => { _SomeFunction(SomeArgs); }, Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
It works fine (sorry if there is any syntax error there, I made it from memory). Therefore, I believe that my problem is with handling the action, which Nick Butler Answer shies away from