genielabs / zwave-lib-dotnet

Z-Wave Home Automation library for .NET / Mono
Apache License 2.0
63 stars 37 forks source link

Serial port stays open after dispose #33

Closed stmax82 closed 6 years ago

stmax82 commented 6 years ago

When I dispose the ZWaveController the serial port sometimes stays open / cannot be opened again.

Sometimes a Thread.Sleep helps before opening the port again, but sometimes it doesn't and the port stays open forever.

Is this a known problem / what can I do about it?

stmax82 commented 6 years ago

Example code:

using System.IO.Ports;
using ZWaveLib;

namespace ConsoleApp1 {
    public class Program {
        public static void Main(string[] args) {
            using (var controller = new ZWaveController("COM4")) {
                controller.Connect();
            } // after this line the port should be closed

            // Thread.Sleep(100); // <-- helps a bit, but not always.

            // try to open the port again
            using (var port = new SerialPort("COM4")) {
                port.Open(); // <-- throws because port is still in use!
            }
        }
    }
}
Bounz commented 6 years ago

Fixed in https://github.com/genielabs/zwave-lib-dotnet/pull/30/commits/7c347b88080adac1c18d1664753d34e57d5474f7