When StartNew crashes, you just do it yourself:
public static Task<T> StartSTATask<T>(Func<T> func) { var tcs = new TaskCompletionSource<T>(); 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 it for Task , which will look almost identical, or add overloads for some of the various options that StartNew has.)
Servy May 23 '13 at 19:56 2013-05-23 19:56
source share