【问题标题】:How to convert 'Run on UI' Aspect from WPF to Windows Forms?如何将“在 UI 上运行”方面从 WPF 转换为 Windows 窗体?
【发布时间】:2015-06-03 07:36:56
【问题描述】:

我试图按照 WPF 示例实现一个方面,但我不知道如何使它适用于 WinForms。

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 的功能?

【问题讨论】:

标签: c# wpf winforms dispatcher postsharp


【解决方案1】:

如果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()));
        }
    }
}

【讨论】:

  • @user1012732:需要更多帮助?
猜你喜欢
  • 2013-02-05
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
相关资源
最近更新 更多