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. catching exceptions thrown from a thread

catching exceptions thrown from a thread

Scheduled Pinned Locked Moved Visual Basic
helpquestion
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.
  • M Offline
    M Offline
    Moraleitor
    wrote on last edited by
    #1

    I'm calling a method from myclass using a thread. How could I catch the exceptions thrown from this thread? Try Dim x As New MyClass Dim obj as new CControlObject obj.param1=1 obj.param2=2 Dim TPool As System.Threading.ThreadPool TPool.QueueUserWorkItem(New System.Threading.WaitCallback _ (AddressOf x.dowhatever), obj) Catch ex As Exception MsgBox("Error: " & ex.ToString) End Try

    D 1 Reply Last reply
    0
    • M Moraleitor

      I'm calling a method from myclass using a thread. How could I catch the exceptions thrown from this thread? Try Dim x As New MyClass Dim obj as new CControlObject obj.param1=1 obj.param2=2 Dim TPool As System.Threading.ThreadPool TPool.QueueUserWorkItem(New System.Threading.WaitCallback _ (AddressOf x.dowhatever), obj) Catch ex As Exception MsgBox("Error: " & ex.ToString) End Try

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      How you have this setup won't work like you want. Your code, as it is now, will only catch exceptions thrown during the Thread setup and start. It won't catch exceptions that happen inside that new thread. Exceptions caught with Try/Catch blocks won't cross thread boundries. Since the code on your main thread can be anywhere when an exception on a second thread is thrown, there is no way to capture an exception using a Try/Catch block. An alternative method for capturing exceptions on seperate threads is to have your threaded code in a class that can raise events. Then, if an exception does occur, try/catch blocks in your threaded code can raise an error event and send the exception data back to your main code through an event handler. That way, all of your error handling code for your threaded component can he handled in one place. RageInTheMachine9532

      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