so I have some code
Task.Factory.StartNew(() => this.listener.Start()).ContinueWith(
(task) =>
{
if (task.IsCompleted)
{
this.status = WorkerStatus.Started;
this.RaiseStatusChanged();
this.LogInformationMessage("Worker Started.");
}
});
When I am testing I am mocking all the dependant objects (namley this.listener.Start()). the problem is that the test finishes executing before ContinueWith can be called. When I debug it gets called fine due to the extra delay of me stepping through code.
so how can I – from the test code in a different assembly – ensure that the code is run before my test hits its asserts?
I could just use Thread.Sleep … but this seems like a really hacky way of doing it.
I guess I am looking for the Task version of Thread.Join.
Consider the following:
What is the value assigned to
a? You cannot tell, unless the result is returned outside the methodFoo()(as the return value, a public property, an event, etc.).The process of “coordinating the actions of threads for a predictable outcome” is called Synchronization.
One of the easiest solutions in your case might be to return the instance of
Taskclass and the use itsWait()method:No need to wait for the first task, because ContinueWith() creates a continuation that executes asynchronously when the target Task completes (MSDN):