0

This following code gives me an error that "Cross-thread operation not valid: Control 'textBox30' accessed from a thread other than the thread it was created on."

private void serialPort1_DataReceived(object sender,S ystem.IO.Ports.SerialDataReceivedEventArgs e)
{

    int bytes = serialPort1.BytesToRead;
    byte[] byte_buffer = new byte[bytes];
    byte[] ar = new byte[20];
    byte[] ssd = new byte[4];
    byte[] ctrl = new byte[20];
    string pp = "";
    string ll = "";
    serialPort1.Read(byte_buffer, 0, bytes);
    int index = byte_buffer[0];

    array[index] = TestSerializer.MarshalToStructureArray(byte_buffer,bytes);
    textBox30.Text = index.ToString();
    serialPort1.Write("N");
    Thread.Sleep(1000);
}

Can any one Suggest a solution?

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
user3952215
  • 95
  • 1
  • 9

1 Answers1

0

This seems like the InvokeRequired issue.

Instead of:

textBox30.Text = index.ToString();

Try:

textBox30.Invoke(s => textBox30.Text = s, index.ToString())

More Info: Here

Community
  • 1
  • 1
Nick
  • 1,799
  • 13
  • 13
  • I write like this if (textBox30.InvokeRequired) { this.Invoke(((MethodInvoker)delegate { textBox30.Text=index.ToString(); })); // return; } – user3952215 Aug 27 '14 at 04:06