Windows Mobile 6 UI更新问题

发布于 2024-11-10 06:18:29 字数 753 浏览 0 评论 0原文

我有一个用 C# 编写的 Windows Mobile 应用程序,它有更多对话框。我想在触发事件时更新对话框。这是代码:

 public void ServerStateChanged()
        {
            // update the interface
            try
            {
                if (this.Focused)
                {
                    this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
                }
            }
            catch (Exception exc)
            {
            }
        }

该代码运行了几次,但随后我通过以下堆栈跟踪得到了此 System.NotSupportedExceptionat Microsoft.AGL.Common.MISC.HandleAr()\r\nat System.Windows.Forms.Control.get_Focused()\r\nat DialTester.Communication.TCPServerView.ServerStateChanged()\r\nat ...

从哪个线程触发该事件有关系吗?因为我不明白问题是什么,为什么它工作了几次然后就崩溃了。

I have a windows mobile application written in C# that has more dialogs. I want to update a dialog, when an event is triggered. Here is the code:

 public void ServerStateChanged()
        {
            // update the interface
            try
            {
                if (this.Focused)
                {
                    this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
                }
            }
            catch (Exception exc)
            {
            }
        }

The code works a few times, but then I get this System.NotSupportedException with this stacktrace: at Microsoft.AGL.Common.MISC.HandleAr()\r\nat System.Windows.Forms.Control.get_Focused()\r\nat DialTester.Communication.TCPServerView.ServerStateChanged()\r\nat ...

Does it matter from which thread is triggered the event? because I can't figure out what the problem is, why it works a few times and then it crashes.

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

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

发布评论

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

评论(2

爱格式化 2024-11-17 06:18:29

或者如下所示的兰巴方式。在我因使用 Control.BeginInvoke 而受到批评之前,BeginInvoke 是线程安全的并且完全异步(该调用会将更新放入 UI 事件队列中)。

    public void ServerStateChanged()  
    {
        this.BeginInvoke((Action)(() =>
        {
            if (this.Focused)
            {
                this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
            }     
        }));
    }

Or the lamba way as below. And before I get criticised for using Control.BeginInvoke, BeginInvoke is threadsafe and is completely asynchronous (the call will put the update on the UI event queue).

    public void ServerStateChanged()  
    {
        this.BeginInvoke((Action)(() =>
        {
            if (this.Focused)
            {
                this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
            }     
        }));
    }
想挽留 2024-11-17 06:18:29

这很可能是跨线程问题。在函数顶部检查 this.InvokeRequired 并做出相应反应肯定会提高函数的安全性。像这样的东西:

public void ServerStateChanged()         
{
    if(this.InvokeRequired)
    {
        this.Invoke(new delegate
        {
            ServerStateChanged();
        }
        return;
    }

    if (this.Focused)                 
    {                     
        this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
    }             
}             

It's likely to be a cross-thread issue. Checking this.InvokeRequired at the top of the function and reacting accordingly would definitely improve the safety of the function. Something like this:

public void ServerStateChanged()         
{
    if(this.InvokeRequired)
    {
        this.Invoke(new delegate
        {
            ServerStateChanged();
        }
        return;
    }

    if (this.Focused)                 
    {                     
        this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
    }             
}             
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文