Advertisement
BradleyUffner

Untitled

Jun 22nd, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9.  
  10. namespace WebApplication1.Controllers
  11. {
  12.     public class HomeController : Controller
  13.     {
  14.         public async Task<ActionResult> Index()
  15.         {
  16.             await MyMethod();
  17.  
  18.             return View();
  19.         }
  20.  
  21.         public async Task MyMethod()
  22.         {
  23.             Debug.WriteLine(SynchronizationContext.Current); // AspNetSyncronizationContext
  24.  
  25.             SynchronizationContext.SetSynchronizationContext(new CustomSyncContext());
  26.             Debug.WriteLine(SynchronizationContext.Current); // WebApplication1.Controllers.HomeController.CustomSyncContext
  27.  
  28.             await SomeAsyncMethod().ConfigureAwait(true);
  29.             Debug.WriteLine(SynchronizationContext.Current); //null
  30.         }
  31.  
  32.         public async Task SomeAsyncMethod()
  33.         {
  34.             await Task.Delay(TimeSpan.FromSeconds(5));
  35.         }
  36.  
  37.         public class CustomSyncContext : SynchronizationContext { }
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement