Threading Problem
-
The code to create thread at the service start event. Dim SerialPort1 As New SerialPort Dim returnstr As String = "" Dim thread1 As New Thread(AddressOf RecieveSerialData) If thread1.IsAlive = False Then thread1.Start() thread1.Priority = ThreadPriority.Highest End If If thread1.IsBackground = False Then 'I am getting an exception in this statement stating the thread is dead. thread1.IsBackground = True End If Public Sub RecieveSerialData() Try Using SerialPort1 As IO.Ports.SerialPort= My.Computer.Ports.OpenSerialPort(b) MessageBox.Show("The " & b & " port is now opened.") Do Dim incomming As String = SerialPort1.ReadLine(b) If incomming Then MessageBox.Show(Now.ToString) End If Loop End Using Catch ex As Exception MessageBox.Show(ex.ToString, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub The code stop the service and thread : If thread1.IsAlive = True Then thread1.Abort() End If If SerialPort1.IsOpen = True Then SerialPort1.Close() Else MessageBox.Show("The service is already stoped.", "Serial Service", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End Sub Here I am getting various exception like (1) The process cannot access COM3 port because it is being used by another process. This exception occurs when the first time service is activated. In the first attempt the stop service procedure executes properly. (2) After the first execution the IsBackground statement gives an exception stating the thread id dead.
-
The code to create thread at the service start event. Dim SerialPort1 As New SerialPort Dim returnstr As String = "" Dim thread1 As New Thread(AddressOf RecieveSerialData) If thread1.IsAlive = False Then thread1.Start() thread1.Priority = ThreadPriority.Highest End If If thread1.IsBackground = False Then 'I am getting an exception in this statement stating the thread is dead. thread1.IsBackground = True End If Public Sub RecieveSerialData() Try Using SerialPort1 As IO.Ports.SerialPort= My.Computer.Ports.OpenSerialPort(b) MessageBox.Show("The " & b & " port is now opened.") Do Dim incomming As String = SerialPort1.ReadLine(b) If incomming Then MessageBox.Show(Now.ToString) End If Loop End Using Catch ex As Exception MessageBox.Show(ex.ToString, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub The code stop the service and thread : If thread1.IsAlive = True Then thread1.Abort() End If If SerialPort1.IsOpen = True Then SerialPort1.Close() Else MessageBox.Show("The service is already stoped.", "Serial Service", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End Sub Here I am getting various exception like (1) The process cannot access COM3 port because it is being used by another process. This exception occurs when the first time service is activated. In the first attempt the stop service procedure executes properly. (2) After the first execution the IsBackground statement gives an exception stating the thread id dead.
Normally you shouldn't set IsBackground after starting it. The thread may well run and exit before it even gets to that place (it's allowed to do that, even though it usually won't happen) But the rest is strange, I hope someone else can help you better.