0
Reply

Help with creating multiple threads

Anton Gildebrand

Anton Gildebrand

Jun 16 2009 7:38 AM
3.8k
Hello!

I have made an application that transmitts RS232 text strings. I have only made two classes so far but there will be more soon.

Here is what the code look like now

         public void Hello()
{
SerialPort port = new SerialPort(
"COM1", 9600, Parity.None, 8, StopBits.One);

port.Open();

port.Write("Hello");

port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);

Thread.Sleep(2000);

port.Write("Tjosilosan");

port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);

port.Close();
}
public void World()
{
SerialPort port = new SerialPort(
"COM1", 9600, Parity.None, 8, StopBits.One);

port.Open();

port.Write("World");

port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);

port.Close();
}

As you can se, class "Hello" sends Hello and then 2 seconds later Tjosilosan.
Works so far. But if a would call "Hello" and "World" at the same time, First Hello would be sent, two seconds later Tjosilosan and then World.

I dont want it to be this way, Hello, and World should be sent at the same time and then 2 seconds after Tjosilosan will be sent.

This might not be the ultimate solution to the problem, but i wanna solve this with MultiThreading. So that class "Hello" is one thread, and class "World" is another thread.

I have looked at this http://msdn.microsoft.com/en-us/library/7a2f3ay4.aspx but don't understand how to do.

Can anyone help me with creating multiple-threads of the classes?