Why C#IOException Occurs?

Asked 3 months ago, Updated 3 months ago, 14 views

Thank you for your help.

In order to find out how many devices are connected to the PC in C#,

Creating an application to view open COM ports.

In order to detect a new connection or disconnect and update the display, we poll using the following methods:

public void startPoring()
    {
        while(true){
            try
            {
                if (this.usbDeviceList.Count!=SerialPort.GetPortNames().Count();)
                {
                    This.usbDeviceList=SerialPort.GetPortNames().ToList();
                    This.dispConnectedDevice.Invoke(Action)=>
                    {
                        This.dispConnectedDevice.Clear();
                        foreach(var deviceName in this.usbDeviceList)
                        {
                            This.dispConnectedDevice.AppendText(deviceName+"\r\n");
                        }
                    }));
                }
            }
            catch
            {

            }
        }

    }

When I actually tested it, I got IOException at SerialPort.GetPortNames().Count() when I disconnected (so I try-catch it).

The MSDN said IOException is thrown if an I/O error occurs, such as when a file fails to read or write, but we do not know why.

It may be a coincidence, but this exception occurs when the device is unplugged.

If anyone knows the cause, please let me know.

c# windows .net usb

2022-09-30 11:59

1 Answers

In your case, an exception may have occurred when the plug-and-play process conflicts with when the registry is rewritten and when the program invokes SerialPort.GetPortNames().

In a separate issue from the question below, there is a Q&A article from the MSDN Forum that says that when you write and debug a call to SerialPort.GetPortNames(), it usually works, but if the registry changes due to a port change or if you restart your PC (laptop in question).
Problem when using SerialPort.GetPortNames

A Microsoft MCC, MVP certified person says SerialPort.GetPortNames() is just listing registry data for HKLM\Hardware\DeviceMap\SerialComm, and that's the answer.
Perhaps SerialPort.GetPortNames() is still just listing registry data.

Your program always calls SerialPort.GetPortNames() in an infinite loop with no waiting time, which increases the probability of problems.

Possible action options include:I recommend the third one because it has many applications.

1.Explore the cause

As Sayuri commented, investigate the cause by examining the properties of the exception information that occurred and was notified.

2. Action to reduce probability easily

a. Terminate processing after successful retrieval, not infinite loop

b. Leave an infinite loop, but wait or sleep at intervals that are considered reasonable between 1 and 5 seconds.   (Think about stopping from outside as soon as you need it)

3.Trigger plug-and-play event notifications instead of polling

Create a list with SerialPort.GetPortNames() for the first time

 A plug-and-play event notification is received on the basis of information related to the following keywords, and information on the list of COM ports is updated on the basis of information notified at the timing.(No SerialPort.GetPortNames() at that time)

RegisterDeviceNotification()/UnregisterDeviceNotification()
 DEV_BROADCAST_HDR
 DEV_BROADCAST_PORT

WM_DEVICECHANGE
 DBT_DEVICEARRIVAL
 DBT_DEVICEREMOVECOMPLETE

It's not straightforward, it's old, but there are articles and program information like the following.
 Check for device change(add/remove) events
 Detecting USB Drive Removal in a C# Program
 Hardware Helper Library for C#


2022-09-30 11:59

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.