Windows Develop Bookmark and Share   
 index > Windows Forms General > Using Threads in Serial port Connection
 

Using Threads in Serial port Connection

Hi, I have a component which is connected to theserial port and I want to send and recive data via the following code. The GUI should be always running and wait to get the data , thats why I used a single thread . there is no problem while the output is printing in a messagebox but I can not use a listbox or any other controls to print the output, so an error message occures. Please let me know if there is any solution

**********************************************Method********************************************

//Method that is used

public void connect()

{

int r = 3;

while (true)

{

try

{

serialPort1.Open();

for (int i = 0; i < 8; i++)

{

arIdea = serialPort1.ReadChar();

listBox1.Items.Add(arIdea.ToString());

}

serialPort1.WriteLine(r.ToString());

serialPort1.Close();

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

Thread.Sleep(1000);

MessageBox.Show(ar[0] + "\n" + ar[1] + "\n" + ar[2] + "\n" + ar[3] + "\n" + ar[4] + "\n" + ar[5] + "\n" + arDevil + "\n" + ar[7] + "\n" + arMusic);

}

*********************************main part*******************************************

//Creating Thread

try

{

Thread test = new Thread(connect);

test.Start();

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

Asemeh  Saturday, June 16, 2007 8:15 PM

use the following code:

Code Snippet

listBox1.Invoke((MethodInvoker)delegate

{

listBox1.Items.Add(ar[i].ToString());

});

The problem is, that you can't update the UI in another thread. Control.Invoke acts like a SendMessage and so,the delegate is invoked in the UI thread instead.

Thomas Danecker  Sunday, June 17, 2007 10:39 AM
As the previous poster said, you have tried to write to the GUI in the worker thread which is a no-no. In some circumstances you can actually lock up the GUI. I have an example entitled, Web Service Consumption and Winform Data Display which shows how to invoke back to the main GUI thread.
OmegaMan  Sunday, June 17, 2007 11:15 AM

use the following code:

Code Snippet

listBox1.Invoke((MethodInvoker)delegate

{

listBox1.Items.Add(ar[i].ToString());

});

The problem is, that you can't update the UI in another thread. Control.Invoke acts like a SendMessage and so,the delegate is invoked in the UI thread instead.

Thomas Danecker  Sunday, June 17, 2007 10:39 AM
As the previous poster said, you have tried to write to the GUI in the worker thread which is a no-no. In some circumstances you can actually lock up the GUI. I have an example entitled, Web Service Consumption and Winform Data Display which shows how to invoke back to the main GUI thread.
OmegaMan  Sunday, June 17, 2007 11:15 AM

You can use google to search for other answers

Custom Search

More Threads

• wsdl.exe.... sounds great... but WHERE IS IT???!??!
• PrintDocument quality dilemma
• Where did Control Arrays go in VB.NET?
• how to display a counterdown clock using C#
• Webbrowser Control HTTP Auth Header With Proxy
• settingskey property
• vScrollBar A Panel?
• Publish in VB2005
• RichTextBox: secure (hide or encrypt) data in memory
• Whiteboard Application