Add item to listbox from another thread

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im developing a chat client and want to display the incoming text in a listbox in the main form, I tried doing this directly in the datareceived event handler of
the serial port, like this:

<font color="#0000ff" size=2>
</font> <font color="#0000ff" size=2>private</font><font size=2>: System::Void
serialPort1_DataReceived(System::Object^ sender,
System::IO::Ports::SerialDataReceivedEventArgs^ e) {</font>
<font size=2></font><font color="#008000" size=2>listBox1->Items->Add("Received: " +
serialPort1->ReadLine());</font> <font color="#008000" size=2></font><font size=2>
</font> <font size=2></font>
<font size=2>}</font>


But I got the "Cross-thread operation not
valid: Control listBox1 accessed from a thread other than the thread it was
created on." Now I know I have to use delegates, I just dont know how. Any help
would be much apreciated.

View the full article
 
Back
Top