跨线程操作在静态方法中无效

时间:2014-02-01 04:45:14

标签: c# multithreading winforms invalidoperationexception

我在WinForms C#app中有以下代码: -

private static void displayTime(object source, ElapsedEventArgs e)
{
    timer++;
    timeTxtBox.Text = parseTime(timer);
}

这会向InvalidOperationException发送包含详细信息的消息

  

跨线程操作无效:控制从其创建的线程以外的线程访问的'timeBox'。

我将如何使这项工作?

1 个答案:

答案 0 :(得分:1)

试试这个:

private static void displayTime(object source, ElapsedEventArgs e)
{ 
    timeTxtBox.Invoke(new Action(() => 
    {
    timer++;
    timeTxtBox.Text = parseTime(timer);
    }));
}
相关问题