closing serialport1 form 1 and form 2
-
Hello, I have two windows forms, form1 and form2. I have placed a button on form1. When I click the button on form1 it will open up form2 via
new Form2().Showdialog()
I have added 2 buttons to my form2 and a serialport. My 2 buttons are called Connect and Disconnect on form2. When I click the Connect button my comm port opens using
serialport1.open()
. When I click Disconnect the port closes using
serialport1.close().
This all works fine until I click
serialport1.open
then close form2 then open form2 via my form1 button again. Keeping in mind that the port is still open or that's my hopes, when I press Disconnect on my form2 my port will not close.
-
Hello, I have two windows forms, form1 and form2. I have placed a button on form1. When I click the button on form1 it will open up form2 via
new Form2().Showdialog()
I have added 2 buttons to my form2 and a serialport. My 2 buttons are called Connect and Disconnect on form2. When I click the Connect button my comm port opens using
serialport1.open()
. When I click Disconnect the port closes using
serialport1.close().
This all works fine until I click
serialport1.open
then close form2 then open form2 via my form1 button again. Keeping in mind that the port is still open or that's my hopes, when I press Disconnect on my form2 my port will not close.
I can think of 2 possibilities: 1. Put the serial port code in a handler class that implements IDisposable and pass it to form2 in the constructor:
Using serialHandler As New SerialHandler
Dim form2 As Form2 = New Form2(serialHandler) form2().Showdialog()
End Using
The Dispose method on the SerialHandler will handle the closing of the serial port if not already closed and dispose of any object that may cause memory leaks. The other benefit is that you have encapsulated serial port handling and simplified Form2's code. Note: If not modal, then store a reference to SerialHandler in Form1 and manually call the Dispose method of the SerialHandler class when Form2 is closed - this will require listening for Form2 closing event. 2. Implement IDisposable in Form2 so that when the form is closed and disposed of, the same applies as the first case. If it was me, I would use Option 1.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
-
I can think of 2 possibilities: 1. Put the serial port code in a handler class that implements IDisposable and pass it to form2 in the constructor:
Using serialHandler As New SerialHandler
Dim form2 As Form2 = New Form2(serialHandler) form2().Showdialog()
End Using
The Dispose method on the SerialHandler will handle the closing of the serial port if not already closed and dispose of any object that may cause memory leaks. The other benefit is that you have encapsulated serial port handling and simplified Form2's code. Note: If not modal, then store a reference to SerialHandler in Form1 and manually call the Dispose method of the SerialHandler class when Form2 is closed - this will require listening for Form2 closing event. 2. Implement IDisposable in Form2 so that when the form is closed and disposed of, the same applies as the first case. If it was me, I would use Option 1.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
-
Thank you MUCH! Are you C# uhm what's the word? Fluent maybe? When I think of serial handler I think of a receive event handler for the serial port. So I'm a little lost on VB.
Your code presented as VB, so I replied in kind. C# works the same with minor variances.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee