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. Visual Basic
  4. Threading Problem

Threading Problem

Scheduled Pinned Locked Moved Visual Basic
help
2 Posts 2 Posters 0 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.
  • D Offline
    D Offline
    DarkSorrow38
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • D DarkSorrow38

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      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