SHARE
TWEET

Untitled

a guest Jan 16th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top