2015-06-03 57 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:需要更多帮助? –