在Windows CE C#程序中访问线程中的控件

在Windows CE C#程序中访问线程中的控件,c#,compact-framework,windows-ce,backgroundworker,invoke,C#,Compact Framework,Windows Ce,Backgroundworker,Invoke,我正在用网络(TCP/IP)和串行端口在C#中为WinCE6编写一个程序。我使用了一个线程来监听套接字,作为收到的数据,我想在表单上的标签中显示它。 但是访问线程中的控件时出错,我想用和Invoke-BeginInvoke命令解决这个问题,但没有成功。都不是幕后工作者 有人能帮忙吗 tnx每当我需要从后台线程更新UI元素时,我都会使用以下代码(在这里更新名为“txtLog”的文本框): 显示您的代码。Explain'not work'@Alan,“not work”表示程序运行时没有任何错误,并

我正在用网络(TCP/IP)和串行端口C#中为WinCE6编写一个程序。我使用了一个线程来监听套接字,作为收到的数据,我想在表单上的标签中显示它。 但是访问线程中的控件时出错,我想用和Invoke-BeginInvoke命令解决这个问题,但没有成功。都不是幕后工作者

有人能帮忙吗


tnx

每当我需要从后台线程更新UI元素时,我都会使用以下代码(在这里更新名为“txtLog”的文本框):


显示您的代码。Explain'not work'@Alan,“not work”表示程序运行时没有任何错误,并且工作正常,但该代码段没有执行,例如标签没有更改!
if(txtcprecieve.InvokeRequired){SetTextCallback d=new settxtcallback(settxt);txtcprecieve.Invoke(d);}委托void SetTextCallback();void SetText(){label3.Text=stringData;}
如果在SetText方法中设置断点,会发生什么情况?它会被执行吗?您是否尝试设置txttprecieve或label3?看起来基本正确。要么您设置了错误的控件文本,要么您的代码从未被工作人员调用。请在代码中设置断点,然后逐步执行
if (txtTCPRecieve.InvokeRequired)
{
    SetTextCallback d = new SetTextCallback(SetText);
    txtTCPRecieve.Invoke(d);
}

delegate void SetTextCallback();

void SetText()
{
    label3.Text = stringData;
}
    delegate void SetTextCallback(string text);
    public void addLog(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.txtLog.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(addLog);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            txtLog.Text += text + "\r\n";
            //scroll to updated text
            txtLog.SelectionLength = 0;
            txtLog.SelectionStart = txtLog.Text.Length - 1;
            txtLog.ScrollToCaret();
        }
    }