I am trying to set the apartment state on a task but see no option in doing this. Is there a way to do this using a Task?
for (int i = 0; i < zom.Count; i++)
{
Task t = Task.Factory.StartNew(zom[i].Process);
t.Wait();
}
I am trying to set the apartment state on a task but see no option in doing this. Is there a way to do this using a Task?
for (int i = 0; i < zom.Count; i++)
{
Task t = Task.Factory.StartNew(zom[i].Process);
t.Wait();
}
When
StartNew
fails you just do it yourself:(You can create one for
Task
that will look almost identical, or add overloads for some of the various options thatStartNew
has.)An overload of Servy's answer to start a void Task
You could for example make a new task as follows:
This is what I'm using with Action since I don't need to return anything:
Where I call it like this:
For details, please see https://github.com/xunit/xunit/issues/103 and Func vs. Action vs. Predicate
FYI, this is the exception I was getting where I needed to set the apartment state: