如何使用抛出 InvalidOperationException 的计时器更改标签内容

发布于 2024-08-29 21:24:51 字数 300 浏览 3 评论 0原文

我正在制作一个应用程序,并在该应用程序中使用计时器来更改 WPF C# .NET 中的标签内容。

在计时器的已用事件中,我正在编写以下代码,

lblTimer.Content = "hello";

但它抛出 InvalidOperationException 并给出消息调用线程无法访问此对象,因为不同的线程拥有它。

我正在使用 .NET Framework 3.5 和 WPF 以及 C#。

请帮助我。
提前致谢。

I'm making an application and I'm using a timer in that application to change label content in WPF C# .NET.

In the timer's elapsed event I'm writing the following code

lblTimer.Content = "hello";

but its throwing an InvalidOperationException and gives a message The calling thread cannot access this object because a different thread owns it.

I'm using .NET framework 3.5 and WPF with C#.

Please help me.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-09-05 21:24:51

对于 .NET 4.0,使用 DispatcherTimer 要简单得多。然后,事件处理程序位于 UI 线程中,它可以直接设置控件的属性。

private DispatcherTimer updateTimer;

private void initTimer
{
     updateTimer = new DispatcherTimer(DispatcherPriority.SystemIdle); 
     updateTimer.Tick += new EventHandler(OnUpdateTimerTick);
     updateTimer.Interval = TimeSpan.FromMilliseconds(1000);
     updateTimer.Start();
}

private void OnUpdateTimerTick(object sender, EventArgs e)
{
    lblTimer.Content = "hello";
}

For .NET 4.0 it is much simpler to use a DispatcherTimer. The eventhandler is then in the UI thread and it can set properties of the control directly.

private DispatcherTimer updateTimer;

private void initTimer
{
     updateTimer = new DispatcherTimer(DispatcherPriority.SystemIdle); 
     updateTimer.Tick += new EventHandler(OnUpdateTimerTick);
     updateTimer.Interval = TimeSpan.FromMilliseconds(1000);
     updateTimer.Start();
}

private void OnUpdateTimerTick(object sender, EventArgs e)
{
    lblTimer.Content = "hello";
}
迷乱花海 2024-09-05 21:24:51

InvokeRequired 在 wpf 中不起作用。

更新另一个线程拥有的 GUI 元素的正确方法是:

在模块级别声明:

delegate void updateLabelCallback(string tekst);

这是更新标签的方法:

private void UpdateLabel(string tekst)
    {
        if (label.Dispatcher.CheckAccess() == false)
        {
            updateLabelCallback uCallBack = new updateLabelCallback(UpdateLabel);
            this.Dispatcher.Invoke(uCallBack, tekst);
        }
        else
        { 
    //update your label here
        }
     }

InvokeRequired doesn't work in wpf.

The proper way the update a GUI element owned by another thread is this :

Declare this on module level :

delegate void updateLabelCallback(string tekst);

This is the method to update your label :

private void UpdateLabel(string tekst)
    {
        if (label.Dispatcher.CheckAccess() == false)
        {
            updateLabelCallback uCallBack = new updateLabelCallback(UpdateLabel);
            this.Dispatcher.Invoke(uCallBack, tekst);
        }
        else
        { 
    //update your label here
        }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文