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
When StartNew fails you just do it yourself:
public static Task StartSTATask(Func func)
{
var tcs = new TaskCompletionSource();
Thread thread = new Thread(() =>
{
try
{
tcs.SetResult(func());
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
(You can create one for Task that will look almost identical, or add overloads for some of the various options that StartNew has.)