如何在ui线程上运行委托?

时间:2012-11-23 05:20:21

标签: windows-runtime windows-store-apps

我在工作线程中(我的意思是,线程池任务)......

我会做Windows手机:

    Deployment.Current.Dispatcher.BeginInvoke(
      delegate () 
      {
        // Do something on ui thread...
      }
    );

如何在Windows应用商店应用中执行此操作?

我通过msdn搜索但是空了......

由于

1 个答案:

答案 0 :(得分:0)

以下是答案:

class ThreadUtility
{
  // Warning: "Because this call is not awaited, execution of the current method 
  // continues before the call is completed. Consider applying the 'await' 
  // operator to the result of the call."
  // But that's what we want here --- just to schedule it and carry on.
  #pragma warning disable 4014 
  public static void runOnUiThread(Windows.UI.Core.DispatchedHandler del)
  {
    CoreWindow window = CoreWindow.GetForCurrentThread();
    window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, del);
  }
  #pragma warning restore 4014
}

... 

    ThreadUtility.runOnUiThread(
      delegate () 
      {
        if(handler != null)
        {
          // Do something on ui thread...
        }
      }
    );