I want to connect 50 USB serial port devices to a single computer and communicate asynchronously (using polling) with each of them.
AFAIK, using C# async/await does not cause the creation of additional threads. However, even though only a single thread is used, the rest of the program maintains responsiveness for non-CPU bound tasks.
For each USB serial port async/await method call, I understand that a new state machine is created by the compiler/CLR.
How many USB serial port methods can be async/awaited before a single thread becomes overloaded? I'm guessing that dynamically creating state machines is CPU heavy. Is the CLR smart enough to offload some of the workload / state machine overhead to other threads or CPU cores?
Example issue: Async/await code development takes place on a new fast computer, but the customer has an old slow computer. Initiating many state machines may work well during development, but perhaps stall upon deployment.
Edit: The following dummy code creates 500 async/awaits, thus causing the creation of 500 state-machines. Compiling and running the code from the command line shows no evidence of increased CPU usage (according to Windows Resource monitor plots).
MyClass[] myClass = new MyClass[numInstances];
for (int i = 0; i < numInstances; i++)
{
myClass[i] = new MyClass();
_ = myClass[i].MyMethod(i);
}
Console.ReadLine();
public class MyClass
{
public async Task MyMethod(int i)
{
Console.WriteLine("Opening port" + i);
await Task.Delay(3000); // Do USB serial port poll
Console.WriteLine("Done" + i);
}
}