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?
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.StartNewcreates the task, then callsStarton your behalf. =DIf you want, you can either
Waiton the task, like @Peter Ritchie said, or you can create the task manually like so: