I have implemented a solution as per this here example where I listen to serial port changes. Normally this worked good but recently I have started stress testing the application and noticed that in certain situations this does not work.
Situation 1:
I have no serial ports. Nothing is connected. The code fails at the deviceAttachWatcher.Start();
. So this is the first point where the code should start listening for changes. The error message is NotSupported. If I look in EventViewer under Applications and Services Logs/Microsoft/Windows/WMI-Activity/Operation I can find an entry with following message:
Id = {00000000-0000-0000-0000-000000000000}; ClientMachine = xyz; User = DOMAIN\user.name; ClientProcessId = 16548; Component = Unknown; Operation = Start IWbemServices::ExecNotificationQuery - root\WMI : select * from __InstanceCreationEvent within 1 where TargetInstance ISA 'MSSerial_PortName'; ResultCode = 0x80041032; PossibleCause = Unknown
Situation 2:
I have one serial port. The code does not fail at deviceAttachWatcher.Start();
. It even goes through the second call deviceDetachWatcher.Start();
. The application is not listening to changes on serial ports but if I connect or disconnect this device nothing happens. I get two entries in the Event Viewer.
- Entry 1
Namespace = root\WMI; NotificationQuery = select * from __InstanceCreationEvent within 1 where TargetInstance ISA 'MSSerial_PortName'; UserName = DOMAIN\user.name; ClientProcessID = 20260, ClientMachine = xyz; PossibleCause = Temporary
- Entry 2
Namespace = root\WMI; NotificationQuery = select * from __InstanceDeletionEvent within 1 where TargetInstance ISA 'MSSerial_PortName'; UserName = DOMAIN\user.name; ClientProcessID = 20260, ClientMachine = xyz; PossibleCause = Temporary
These two event entries correspond to my two queries.
Situation 3:
I have 2 serial ports connected. Code works as expected.
QUESTION
Obviously, I would like the situation 3 to be the constant. What is happening in situations 1 and 2? Why am I getting "NotSupported" error?
Initially I had this problem as well but I fixed it as per answer so now I do not get this log entry in the Event Viewer anymore(this one was under Windows Logs/System).
UPDATE
I just tested this on my colleagues machine and he has the same issue so environment as a problem is out.