RunSynchronously may not be called on task that was already started
Solution 1
The problem is that you started the task when you call TaskFactory.StartNew
- I mean, it's even in the name of the method that you are starting the task. StartNew
creates the task, then calls Start
on your behalf. =D
If you want, you can either Wait
on the task, like @Peter Ritchie said, or you can create the task manually like so:
var task = new Task(() => { ... });
task.RunSynchronously();
Solution 2
It's already started, just use myTask.Wait()
amateur
Updated on October 19, 2020Comments
-
amateur over 3 years
I am having an issue with a c# class I created for unit testing my application, in particular the issue is around a System.Threading.Tasks.Task object.
I have a list of such objects and on them I want to execute each synchronously.
I call the following:
myTask.RunSynchronously();
When I do such, I am always getting the following error and I dont know why are how I can fix it.
System.InvalidOperationException: RunSynchronously may not be called on task that was already started.
Anyone got any ideas?