0
votes

I am troubleshooting an issue with an application that monitors a COM port in which the application hangs when I try to close it. On my own machine it closes just fine. I am testing this app on a terminal server, and I have COM ports shared via Remote Desktop. Over the Remote Desktop session the app is hanging infinitely and cannot be closed via task manager "End Process" either.

The basic code structure for the monitoring is in vb.net and I am using System.IO.Ports.SerialPort. It uses the SerialPort.BaseStream.BeginRead and SerialPort.BaseStream.EndRead as the methods to grab information off of the port. It calls these methods infinitely using an Action with a Callback until a CancellationToken.IsCancellationRequested occurs to disrupt the process.

It turns out that when I try to cancel the loop via CancellationToken.Cancel(), the BeginRead method is already blocking and my cancellation is not able to get through. I discovered that the ReadTimeout setting built-in to the SerialPort class does not apply to BaseStream.BeginRead. This is only an issue in the Remote Desktop session. The Remote server's operating system is Windows Server 2008 R2 Standard. I've tried other remote servers with windows 8 operating system and those ones do not have the problem.

1

1 Answers

-1
votes

The internet was not helpful in this case, but after a lot of tinkering I discovered a method that fixes the problem.

In the end I needed to call SerialPort.DiscardInBuffer() before calling my CancellationToken.Cancel().

Hopefully this helps someone else in the future.