• .NET serial port datareceived fires slow?

    Today I discovered something that had been a pain in my .NET project for weeks. If you work with the SerialPort component in .NET 4 you might notice that in some cases it takes several hundreds of milliseconds between data arriving in your serial port buffer, and your datareceived handler being called. I couldn’t trace this until I started doing some debugging with a port monitor like HDD free serial port monitor, and reading through this threadat MSDN forums. I figured that the delay occurred when the handler was queued for execution through the threadpool. Under certain circumstances the threadpool creates a new thread in order to handle your routine, and apparently this may take up to 500 milliseconds.

    So what you should do is use Threadpool.SetMinThreads() at program startup to increase the minimum number of threads to a suitable value, and there you are! No more delay!

    Thanks to Alex Pinsker and ‘nobugz’.


     Leave a reply