从非UI线程中的另一个类更新UI

时间:2018-03-15 15:39:16

标签: c# ui-thread begininvoke

我想从非UI线程中的另一个类更新UI。

但我不能直接在Test类中调用“BeginInvoke”。

如何解决......

private void Form1_Load(object sender, EventArgs e)
{
    Task.Run(() =>
    {
        Test.Set(textBox1);
    });
}

public class Test
{       
    public static void Set(TextBox textBox)
    {
        // ↓Exception...
        textBox.Text = "ABCD";
    }
}

1 个答案:

答案 0 :(得分:-1)

根据AlexeiLevenkov的回复。

我修改了我的代码,如下所示,效果很好。

public static void Set(TextBox textBox, string text)
{
        textBox.BeginInvoke(new Action(() => { textBox.Text = text; }));
}
相关问题