2015-06-03 87 views
-2

我試圖實現WPF示例後的一個方面,但我無法弄清楚如何使它適用於WinForms。如何將'在UI上運行'從WPF轉換爲Windows窗體?

class RunOnUIThreadAttribute : IMethodInterceptionAspect 
{ 
    public override void OnInvoke(MethodInterceptionArgs args) 
    { 
     DispatcherObject dispatchedObj = (DispatcherObject)args.Instance; 
     if (dispatchedObj.CheckAccess()) 
     { 
      args.Proceed(); 
     } 
     else 
     { 
      dispatchedObj.Dispatcher.Invoke((Action)(() => args.Proceed())); 
     } 
    } 
} 

如何獲得在Windows窗體上工作的Dispatcher的等效項?

+1

你知道如何切換到UI線程Windows窗體中一個方面的外? – nvoigt

+0

有用的SO鏈接:http://stackoverflow.com/questions/4928195/c-sharp-windows-forms-application-updating-gui-from-another-thread-and-class和http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in- – seveves

回答

2

如果args.InstanceControl,你可以使用InvokeRequiredInvoke,而不是他們WPF同行:

class RunOnUIThreadAttribute : IMethodInterceptionAspect 
{ 
    public override void OnInvoke(MethodInterceptionArgs args) 
    { 
     Control c = (Control)args.Instance; 
     if (!c.InvokeRequired) 
     { 
      args.Proceed(); 
     } 
     else 
     { 
      c.Invoke((Action)(() => args.Proceed())); 
     } 
    } 
} 
+0

@ user1012732:需要更多幫助? –