title: Kill a BackgroundWorker in VB.net if thread hangs - using COM object
body: <p>I have an application that interacts with third party COM objects. A method of the COM object that I call will sometimes stop responding. I created a Backgroundworker thread to call the function and I am trying to monitor it from my main thread to kill it if it hangs. I have additional code (not supplied) that tracks the processing time using system.timer from the main thread and I raise an event if it exceeds my threshold (this part is fine). This is when I want to kill the thread and stop code execution. The problem is.. if I use the cancelasync method it will just pend since the code execution is stuck on the function call.</p>
  
  <p>The particular function call in the code snippet that hangs is "objCOM.SendDataToServer()". It typically takes 1-3 seconds to return, but if it gets no response it will just hang indefinitely and won't return at all (no errors just hangs).. there is no timeout... and since I don't have access to the source function I cannot supply one. I tried the .dispose() method of the thread, but apparently that doesn't kill it and neither does cancelasync. I just need help figuring out how to KILL this thread so I can reset the server connection and call the function again. Any help is MUCH appreciated!</p>
  
  <pre><code>Public Class COMobject
      Private objCOM as new acs.manager
  
      Public Sub CallComFunction()
          Dim bw_com as New Backgroundworker
          AddHandler bw_com.DoWork, AddressOf bw_com_dowork
          AddHandler bw_com.RunWorkerCompleted, AddressOf bw_com_runworkercompleted
          AddHandler bw_com.ProgressChanged, AddressOf bw_com_progresschanged
  
          bw_com.WorkerReportsProgress = True
          bw_com.RunWorkerAsync()
      End Sub
  
      Private Sub bw_com_dowork(byval sender as object,byval e as doworkeventargs)
          'this is the long running function that will hang
          call objCOM.SendDataToServer()
      End sub
  End Class
  </code></pre>