如何写一个PostSharp调用方面简化跨线程控制更新 [英] How to write a PostSharp Invoke aspect to simplify cross thread control updates

查看:88
本文介绍了如何写一个PostSharp调用方面简化跨线程控制更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想在线程更新控制这是怎么了,我一般最终会做它:

When I want to update controls across threads this is how I generally end up doing it:

this.Invoke((MethodInvoker)delegate { SomeProcedure(); });



建议的方式做到这一点实际上是调用调用您要更新的特定控制,但99%的时间形式(即本在我的例子)和控制将要被同一线程上创建的,所以我真的很喜欢这样的方式为简单起见。

The suggested way to do it is actually to call the invoker for the specific control you want to update, but 99% of the time the form (i.e. 'this' in my example) and the control are going to have been created on the same thread so I really like doing this way for simplicity's sake.

我在想,虽然这将是很好,如果我刚做了一个PostSharp方面穿上SomeProcedure的顶部,将它包装在一个声明中的那件事情对我来说。

I was thinking though that it would be nice if I just had a PostSharp aspect to put on top of SomeProcedure that would wrap it in that mess of a statement for me.

和去... ...(噢,100的奖金分给第一个可用的答案:)

And go... (oh yeah, 100 bonus pts to first usable answer :)

推荐答案

我没有对之前的WinForms程序线程访问,但我已经有PostSharp + Silverlight的做到了。因此,与有点谷歌搜索,我给它一个镜头。 !不能保证它的工作原理虽然

I haven't programmed thread access before on WinForms, but I have done it with PostSharp + Silverlight. So with a bit of googling, I'll give it a shot. No guarantees that it works though!

[Serializable]
public class OnGuiThreadAttribute : MethodInterceptionAspect
{
    private static Control MainControl;

    //or internal visibility if you prefer
    public static void RegisterMainControl(Control mainControl) 
    {
        MainControl = mainControl;
    }

    public override void OnInvoke(MethodInterceptionArgs eventArgs)
    {
        if (MainControl.InvokeRequired)
            MainControl.BeginInvoke(eventArgs.Proceed);
        else
            eventArgs.Proceed();
    }
}

和想法是在你的应用程序的开始,注册属性主/根的控制。然后你想要的任何方法,以确保在主线程上运行,只需用装修[OnGuiThread] 。如果它已经在主线程,它只是运行方式。如果它不是,它会促进方法调用作为代表到主线程异步

And the idea is at the start of your application, register your main/root control with the attribute. Then any method you want to ensure runs on the main thread, just decorate with [OnGuiThread]. If it's already on the main thread, it just runs the method. If it's not, it will promote the method call as a delegate to the main thread asynchronously.

编辑:我只是想通了,你问到(这是已故)使用您正在使用的目标控制的具体方法调用。假设你装饰的例如的在子类的控制方式:

I just figured out (it's late) that you're asking to use the specific invoking method for the target control you're using. Assuming that you decorate instance methods on subclasses for your controls:

[Serializable]
public class OnGuiThreadAttribute : MethodInterceptionAspect
{
    public override void OnInvoke(MethodInterceptionArgs eventArgs)
    {
        //you may want to change this line to more gracefully check 
        //if "Instance" is a Control
        Control targetControl = (Control)eventArgs.Instance;

        if (targetControl.InvokeRequired)
            targetControl.BeginInvoke(eventArgs.Proceed);
        else
            eventArgs.Proceed();
    }
}

这篇关于如何写一个PostSharp调用方面简化跨线程控制更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆