Windows Mobile 6 UI更新问题

| 我有一个用C#编写的Windows Mobile应用程序,其中包含更多对话框。我想在触发事件时更新对话框。这是代码:
 public void ServerStateChanged()
        {
            // update the interface
            try
            {
                if (this.Focused)
                {
                    this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
                }
            }
            catch (Exception exc)
            {
            }
        }
该代码工作了几次,但随后我得到了这个带有堆栈跟踪的
System.NotSupportedException
at Microsoft.AGL.Common.MISC.HandleAr()\\r\\nat System.Windows.Forms.Control.get_Focused()\\r\\nat DialTester.Communication.TCPServerView.ServerStateChanged()\\r\\nat ...
从哪个线程触发事件有关系吗?因为我无法弄清楚问题出在哪里,为什么它工作几次,然后崩溃。     
已邀请:
        这可能是跨线程问题。选中功能顶部的
this.InvokeRequired
并作出相应反应肯定可以提高功能的安全性。像这样:
public void ServerStateChanged()         
{
    if(this.InvokeRequired)
    {
        this.Invoke(new delegate
        {
            ServerStateChanged();
        }
        return;
    }

    if (this.Focused)                 
    {                     
        this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
    }             
}             
    
        或如下所示的lamba方式。在因使用Control.BeginInvoke而受到批评之前,BeginInvoke是线程安全的,并且是完全异步的(调用会将更新放入UI事件队列中)。
    public void ServerStateChanged()  
    {
        this.BeginInvoke((Action)(() =>
        {
            if (this.Focused)
            {
                this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
            }     
        }));
    }
    

要回复问题请先登录注册