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
If Task needs to be provided with a CancellationToken, STA thread can be started within a task action and joined as shown below:
var task = Task.Factory.StartNew(() => { var tcs = new TaskCompletionSource(); var thread = new Thread(() => { try { action(); tcs.SetResult(new object()); } catch (Exception e) { tcs.SetException(e); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); }, cancellationToken);