Hello,
I am currently discovering the wonderful world of multithreading and I am having trouble. On my Main class I am using a SerialPort, handling SerialPort1_DataReceived to read received data. Everything worked ok until I tried to send a heavy function in a new thread to be able to show a nice spinner image on the UI (main form) while executing this function. However, this heavy function uses the result of data received through the serialPort and this is where it gets tricky:
Here is how a sample code of what I am doing:
In my main form, I open the Serialport when clicking on "connect" button
In the main form, when I click on another button I show the spinner and start my new thread with my heavy function
and then finally in Export.vb, the class with my heavy function:
As you can see, I have several msgBox to tell me if the serialPort is Open. Before starting the thread it is open, and right after starting the new thread, the serialPort is closed ! This results that I can't handle data received by the serialPort while being in my new thread.
Do you know why launching a new thread closes my serialPort conection ?
Thank you very much in advance
I am currently discovering the wonderful world of multithreading and I am having trouble. On my Main class I am using a SerialPort, handling SerialPort1_DataReceived to read received data. Everything worked ok until I tried to send a heavy function in a new thread to be able to show a nice spinner image on the UI (main form) while executing this function. However, this heavy function uses the result of data received through the serialPort and this is where it gets tricky:
Here is how a sample code of what I am doing:
In my main form, I open the Serialport when clicking on "connect" button
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click Try 'Get the port name from the application list box. SerialPort1.PortName = lstCOMPorts.Items(lstCOMPorts.SelectedIndex).ToString() 'Open the COM port. SerialPort1.Open()[INDENT]Catch ex As Exception [/INDENT] btnClose_Click(Me, e) End Try End Sub
In the main form, when I click on another button I show the spinner and start my new thread with my heavy function
Private Sub BtGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtGo.Click BtGo.Enabled = False Dim Export1 As New Export(RadioGPX.Checked) Dim thread1 As New System.Threading.Thread(AddressOf Export1.Go) thread1.SetApartmentState(Threading.ApartmentState.STA) AddHandler Export1.FinishedExporting, AddressOf FinishedExportingEventHandler setSpinnerVisible(True) 'show the spinner MsgBox(Me.SerialPort1.IsOpen.ToString) 'Verify if the port is open --> True thread1.Start() End Sub
and then finally in Export.vb, the class with my heavy function:
Public Sub Go() MsgBox(Main.SerialPort1.IsOpen.ToString) 'check is the serial port is open ---> FALSE 'heavy function... End Sub
As you can see, I have several msgBox to tell me if the serialPort is Open. Before starting the thread it is open, and right after starting the new thread, the serialPort is closed ! This results that I can't handle data received by the serialPort while being in my new thread.
Do you know why launching a new thread closes my serialPort conection ?
Thank you very much in advance
Last edited: