/// <inheritdoc/> public Task StartSTATask(Action func) { var tcs = new TaskCompletionSource <object>(); var thread = new Thread(() => { var id = CurrentThread.GetId(); try { _pool.Add(id); func(); tcs.SetResult(null !); } catch (Exception e) { tcs.SetException(e); } finally { _pool.Remove(id); } }); thread.SetApartmentState(ApartmentState.STA); if (_pool.Count > _maxNumberOfModals) { tcs.SetResult(null !); return(tcs.Task); } thread.Start(); return(tcs.Task); }
/// <inheritdoc/> public Task <TArg> StartSTATask <TArg>(Func <TArg> func) where TArg : class { var tcs = new TaskCompletionSource <TArg>(); var thread = new Thread(() => { var id = CurrentThread.GetId(); try { _pool.Add(id); tcs.SetResult(func()); } catch (Exception e) { tcs.SetException(e); } finally { _pool.Remove(id); } }); thread.SetApartmentState(ApartmentState.STA); if (_pool.Count > _maxNumberOfModals) { tcs.SetResult(default(TArg) !); return(tcs.Task); } thread.Start(); return(tcs.Task); }