Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. closing serialport1 form 1 and form 2

closing serialport1 form 1 and form 2

Scheduled Pinned Locked Moved Windows Forms
winforms
4 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    geomeo123
    wrote on last edited by
    #1

    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.

    Graeme_GrantG 1 Reply Last reply
    0
    • G geomeo123

      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.

      Graeme_GrantG Offline
      Graeme_GrantG Offline
      Graeme_Grant
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • Graeme_GrantG Graeme_Grant

        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

        G Offline
        G Offline
        geomeo123
        wrote on last edited by
        #3

        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.

        Graeme_GrantG 1 Reply Last reply
        0
        • G geomeo123

          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.

          Graeme_GrantG Offline
          Graeme_GrantG Offline
          Graeme_Grant
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups