zoukankan      html  css  js  c++  java
  • await 与 SynchronizationContext 关系

    static async Task DoStep()
    {
      //step 1 Debug.WriteLine(
    "DoStep Start thread id: " + System.Threading.Thread.CurrentThread.ManagedThreadId + "|" + System.Threading.Thread.CurrentThread.IsThreadPoolThread); await Task.Run(async () => {   Debug.WriteLine("Task thread id: " + System.Threading.Thread.CurrentThread.ManagedThreadId + "|" + System.Threading.Thread.CurrentThread.IsThreadPoolThread); });
    //这里会出现线程上下文切换
      //如果
    SynchronizationContext.Current is not null,就使用await之前的线程,与 step1保持一致,注意不要再挂起调用前的线程,否则会出现死锁。
      //如果 SynchronizationContext.Current is null,就使用线程池中处理await的线程,与 step1 不保持一致,注意在这之后不能直接使用UI控件,只能通过this.BeginInvoke调用。
      //如Task.Run之后配置 ConfigureAwait(false),就使用线程池中处理await的线程,与 step1 不保持一致,注意在这之后不能直接使用UI控件,只能通过this.BeginInvoke调用。
      Debug.WriteLine("DoStep End thread id: " + System.Threading.Thread.CurrentThread.ManagedThreadId + "|" + System.Threading.Thread.CurrentThread.IsThreadPoolThread); 
       Debug.WriteLine(SynchronizationContext.Current == null ? "null" : SynchronizationContext.Current.ToString());  
    }
  • 相关阅读:
    docker一些基本操作
    Error requesting socket: exit status 255(一个很不错的解决办法)【转】
    十五周至十八周的任务进度
    7月24号day16总结
    7月23号day15总结
    7月22号day14总结
    7月21号day13总结
    7月20号day12总结
    7月19日day11总结
    7月18号day10总结
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/5511898.html
Copyright © 2011-2022 走看看