Hi there,

I'm new in the .NET world, just trying to port over to the .NET platform from VS6 (Visual Basic 6).

I wish to close the main Form with the "x" button while it's Connected to the Serial Port, but the whole form freezes (hangs) without any error.

Any help would be much appreciated, thanks!

Code:
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel

Public Class frmMAIN

    Dim myPorts As Array
    Delegate Sub SetTextCallBlack(ByVal [text] As String)

    Public Sub serDisconnect()

        If ser.IsOpen = True Then
            ser.Close()
            cmdConnect.Enabled = True
            cmdDisconnect.Enabled = False
            cboPorts.Enabled = True
            cboBaud.Enabled = True
        End If

    End Sub

    Private Sub frmMAIN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Text = "Serial Test"

        cmdConnect.Enabled = False

        myPorts = IO.Ports.SerialPort.GetPortNames()

        With cboPorts
            .Items.AddRange(myPorts)
            If .Items.Count() > 0 Then
                .SelectedIndex = 0
            Else
                cmdConnect.Enabled = False
                cmdDisconnect.Enabled = False
                cboPorts.Enabled = False
                cboBaud.Enabled = False
            End If
        End With

        cboBaud.SelectedIndex = 0

        If cboPorts.Items.Count <= 0 Then
            cmdConnect.Enabled = False
        Else
            cmdConnect.Enabled = True
        End If

    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click

        ser.BaudRate = cboBaud.Text
        ser.PortName = cboPorts.Text

        If ser.IsOpen = False Then
            ser.Open()
            cmdConnect.Enabled = False
            cmdDisconnect.Enabled = True
            cboPorts.Enabled = False
            cboBaud.Enabled = False
        End If

    End Sub

    Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click

        Call serDisconnect()

    End Sub

    Private Sub ser_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ser.DataReceived

        ReceivedText(ser.ReadExisting())

    End Sub

    Private Sub ReceivedText(ByVal [text] As String)

        If txtOutput.InvokeRequired Then
            Dim x As New SetTextCallBlack(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            lblPotVal.Text = [text]
            txtOutput.Text &= [text]
        End If

    End Sub

    Private Sub frmMAIN_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Call serDisconnect()
        Application.Exit()
        End
    End Sub

End Class