I am need to listen to all the serial ports in my machine. Say if my machine has 4 serial ports, i have to create 4 threads and start listening to each port with the attache
You could use the thread pool:
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
ThreadPool.QueueUserWorkItem(state =>
{
// This will execute in a new thread
});
}
or create and start the threads manually:
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
new Thread(() =>
{
// This will execute in a new thread
}).Start();
}