locked
How to abort all invoked threads before exiting application? RRS feed

  • Question

  •  I am threading challenged, never having run into the issue in VB6

    Clueless question... Is there any way to abort all invoked threads without specifying a particular thread since I am closing the application anyway?

    In debug mode, after my application closes I am getting "Invoke or BeginInvoke cannot be called on a control until the window handle has been created".

    I am not doing any sort of direct thread handling in code that calls Invoke or BeginInvoke, so I would want to do some sort of "Abort all threads" call, then close the application.
    Tuesday, November 4, 2008 9:10 PM

Answers

  • Use the Join method on your thread. This will join the thread onto your main thread, allowing you to gracefully terminate the thread.

    It does, however, seem you may be using threads inappropriately. A lot of invocation tends to imply a design issue, although this may not necessarily be the case.

    Another alternative is in the Form which is terminating is to have a private 'terminating' flag set when you start closing the form. In the routine which is being invoked from this other thread, check this flag. If it's true then don't invoke anything.



    Stephen J Whiteley
    Tuesday, November 4, 2008 9:41 PM
  • Your threads keep running after the main UI thread has exited.  That bombs when they call Invoke().  A quick fix is to set the Thread.IsBackground property to true.
    Hans Passant.
    Wednesday, November 5, 2008 5:35 AM

All replies

  • Use the Join method on your thread. This will join the thread onto your main thread, allowing you to gracefully terminate the thread.

    It does, however, seem you may be using threads inappropriately. A lot of invocation tends to imply a design issue, although this may not necessarily be the case.

    Another alternative is in the Form which is terminating is to have a private 'terminating' flag set when you start closing the form. In the routine which is being invoked from this other thread, check this flag. If it's true then don't invoke anything.



    Stephen J Whiteley
    Tuesday, November 4, 2008 9:41 PM
  • Your threads keep running after the main UI thread has exited.  That bombs when they call Invoke().  A quick fix is to set the Thread.IsBackground property to true.
    Hans Passant.
    Wednesday, November 5, 2008 5:35 AM