Marco Morgia

Marco Morgia

  • 1.6k
  • 68
  • 1.2k

Class destroyed automatically

Apr 30 2024 6:01 AM

Good morning,

in a project I have to listen on a serial port indefinitely.

To connect to the serial port and read data using this piece of code

SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived);

serialPort.Open();

In the method DataReceived the received data are stored in a Database with an event.

Everything works, but sometimes the class that opens the COM port is destroyed.

The above piece of code is launched on a separate Thread, so as not to block the execution of the main program.

The call that starts the Thread is made by a static class (maybe that can make a difference).

Do you know why the class is automatically destroyed?

Thanks


Answers (3)