I declared a method which should run a Task
asynchronously, then, when the Task
finishes, run the specified delegate synchronously.
C# example:
private void WaitUntilLoadedAsync(Process p, Action callback, int timeout = 1500)
{
Task.Factory.StartNew(() => ProcessUtil.WaitUntilLoaded(p, timeout)).
ContinueWith(() => callback.Invoke()).RunSynchronously();
}
VB.Net Example:
Private Sub WaitUntilLoadedAsync(ByVal p As Process,
ByVal callback As Action,
Optional ByVal timeout As Integer = 1500)
Task.Factory.StartNew(Sub() ProcessUtil.WaitUntilLoaded(p, timeout)).
ContinueWith(Sub() callback.Invoke()).RunSynchronously()
End Sub
However, the RunSynchronously
method throws a System.InvalidOperationException
exception teling me that a continuation Task
cannot be ran synchronouslly.
Then, how I could do it?.
Note(s):
I'm able to support
Async
/Await
solutions.I will preserve the "tiny" logic or cyclomatic complexity of my method, I mean, it is just a method on which I pass an
Acton
, no delegates are declared outside the method or other complex things that could cause the end-user to write more than anAction
. The method itself should automate all the required operations to perform that continuation task synchronouslly, no complexity outside.