0

i'm writing a program for WinCE6 in C# , using Network (TCP/IP) and Serialport. i used a thread for socket listening and as data received i want to show it in a label on form. but there is an error for accessing controls in Threads and i wanted to solve it with and Invoke - BeginInvoke command but didn't work. Neither BackgroundWorker!

if (txtTCPRecieve.InvokeRequired)
{
    SetTextCallback d = new SetTextCallback(SetText);
    txtTCPRecieve.Invoke(d);
}

delegate void SetTextCallback();

void SetText()
{
    label3.Text = stringData;
}

is there any one can help?

tnx

Alan
  • 7,875
  • 1
  • 28
  • 48
Mostafa
  • 13
  • 7
  • Show your code. Explain 'didn't work' – Alan Mar 10 '14 at 15:08
  • @Alan, "didn't work" means program has run without any errors and worked correctly but that piece of codes didn't execute, for example the label didn't change! `if (txtTCPRecieve.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); txtTCPRecieve.Invoke(d); } delegate void SetTextCallback(); void SetText() { label3.Text = stringData; } ` – Mostafa Mar 10 '14 at 19:39
  • What happens if you put a breakpoint in your SetText method? Does it get executed? – Alan Mar 10 '14 at 20:42
  • Are you trying to set txtTCPRecieve or label3? – Alan Mar 10 '14 at 20:57
  • @Alan, there is no error but neither no executing! – Mostafa Mar 10 '14 at 20:58
  • 1
    It looks basically correct. Either you are setting the wrong control's text or your code is never getting called from your worker. **put breakpoints in your code and step through it** – Alan Mar 10 '14 at 21:01
  • is there any difference? i want to show it on a label3.Text or txtRCPRevieve.Text – Mostafa Mar 10 '14 at 21:02
  • Not really as long as you know where it's going. – Alan Mar 10 '14 at 22:49
  • It may be a stupid note, but I don't see where stringData is updated in your code. – Valter Minute Mar 11 '14 at 07:12
  • @Alan, it's executing and the break point hinted, there it is, look it: `Function: SmartDeviceProject1.Form1.SetText(), Thread: 0x8270006 Main Thread` @ValterMinute, stringData is updated in a thread written for Listen form socket... – Mostafa Mar 11 '14 at 14:49
  • If SetText is being called, then you are not setting the right label or the text is blank or unexpected. Go locate the label3 control, is it the correct control? What is stringData in the debugger? is it what you expect? Is the UI thread being blocked by other processing? (can you move the window around freely or is it locked?) Do you have like white text on a white background? – Alan Mar 11 '14 at 16:52
  • i think problem was for these: `Form1 p = new Form1(); p.Thread();` i change it to `Thread();` and the code **josef** said executed correctly! – Mostafa Mar 12 '14 at 09:28

1 Answers1

1

Everytime I need to update UI elements from a background thread I use the following code (here to update a textbox named "txtLog":

    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();
        }
    }
josef
  • 5,951
  • 1
  • 13
  • 24