问题
I want to make serial port based project i have code like this .when i try to close my form it become not responding and hang my app please give suggestion and correct my mistakes in my code thank you in advance
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim bw As BackgroundWorker
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadLine()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.Label1.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.BeginInvoke(x, New Object() {(text)})
Else
Me.Label1.Text = ([text] / My.Settings.deci)
End If
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SerialPort1.PortName = My.Settings.comport 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = My.Settings.baud
'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
End Sub
End Class
来源:https://stackoverflow.com/questions/64573461/serial-port-project-form-close-not-responding