Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. public static Task<T> StartSTATask<T>(Func<T> func)
  2. {
  3. var tcs = new TaskCompletionSource<T>();
  4. Thread thread = new Thread(() =>
  5. {
  6. try
  7. {
  8. tcs.SetResult(func());
  9. }
  10. catch (Exception e)
  11. {
  12. tcs.SetException(e);
  13. }
  14. });
  15. thread.SetApartmentState(ApartmentState.STA);
  16. thread.Start();
  17. return tcs.Task;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement