Guest User

Untitled

a guest
Jan 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. public Task SomeAsyncMethod()
  2. {
  3. var tcs = new TaskCompletionSource();
  4. ... do something, NOT setting the TaskCompletionSource...
  5.  
  6. return tcs.Task
  7. }
  8.  
  9. public void Callsite1()
  10. {
  11. await SomeAsyncMethod();
  12. Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
  13. }
  14.  
  15. public void Callsite2()
  16. {
  17. SomeAsyncMethod().ContinueWith((task) =>
  18. {
  19. Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
  20. });
  21. }
  22.  
  23. Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
  24. tcs.SetResult();
  25.  
  26. public void Callsite1()
  27. {
  28. await SomeAsyncMethod().ConfigureAwait(true or false);
  29. }
  30.  
  31. // Set the result on a threadpool thread, so any synchronous continuations
  32. // will execute in the background.
  33. Task.Run(() => tcs.TrySetResult(result));
  34.  
  35. // Wait for the TCS task to complete; note that the continuations
  36. // may not be complete.
  37. tcs.Task.Wait();
  38.  
  39. public static void TrySetResultWithBackgroundContinuations<TResult>(
  40. this TaskCompletionSource<TResult> @this, TResult result);
Add Comment
Please, Sign In to add comment