写一个线程安全的访问方式到Windows最短路径控制形式 [英] Shortest way to write a thread-safe access method to a windows forms control

查看:117
本文介绍了写一个线程安全的访问方式到Windows最短路径控制形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这篇文章:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80)的.aspx

笔者使用以下方法,使线程安全调用Windows窗体控件:

The author uses the following method to make thread-safe calls to a Windows Forms control:

private void SetText(string text)
{
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.textBox1.InvokeRequired)
    {    
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { text });
    }
    else
    {
        this.textBox1.Text = text;
    }
}

有没有完成同样的事情较短的方式?

Is there a shorter way to accomplish the same thing?

推荐答案

C#3.0和之后:

这是扩展方法一般会是要走的路,因为你总是会想对其执行<一个动作href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.aspx\"><$c$c>ISynchronizeInvoke接口实现,这是一个很好的设计选择。

An extension method would generally be the way to go, since you're always going to want to perform an action on an ISynchronizeInvoke interface implementation, it's a good design choice.

您也可以利用匿名方法(闭包)占的事实,你不知道什么参数传递给扩展方法;关闭将捕获所需的所有的状态。

You can also take advantage of anonymous methods (closures) to account for the fact that you don't know what parameters to pass to the extension method; the closure will capture the state of everything needed.

// Extension method.
static void SynchronizedInvoke(this ISynchronizeInvoke sync, Action action)
{
    // If the invoke is not required, then invoke here and get out.
    if (!sync.InvokeRequired)
    {
        // Execute action.
        action();

        // Get out.
        return;
    }

    // Marshal to the required context.
    sync.Invoke(action, new object[] { });
}

然后你会这样称呼它:

You'd then call it like this:

private void SetText(string text)
{
    textBox1.SynchronizedInvoke(() => textBox1.Text = text);
}

在这里,关闭是在文本参数,即状态被捕获并作为的 动作委托传递给扩展方法。

Here, the closure is over the text parameter, that state is captured and passed as part of the Action delegate passed to the extension method.

之前,C#3.0:

您不必拉姆达前pressions的奢侈品,但你仍然可以概括code。这是pretty大同小异,但不是一个扩展方法:

You don't have the luxury of lambda expressions, but you can still generalize the code. It's pretty much the same, but not an extension method:

static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action)
{
    // If the invoke is not required, then invoke here and get out.
    if (!sync.InvokeRequired)
    {
        // Execute action.
        action();

        // Get out.
        return;
    }

    // Marshal to the required context.
    sync.Invoke(action, new object[] { });
}

然后你用匿名方法的语法调用它:

And then you call it with anonymous method syntax:

private void SetText(string text)
{
    SynchronizedInvoke(textBox1, delegate() { textBox1.Text = text; });
}

这篇关于写一个线程安全的访问方式到Windows最短路径控制形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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