Stop Thread and exit program

My program has a form and another worker class.

Worker class has a thread that receiving data, in loop: socket.ReceiveFrom(....)
When I close the form the program (thread) is still running.

How to stop everything and exit





Answer this question

Stop Thread and exit program

  • Mike9000

    So in your form's Dispose method or OnClosing event you should call a stop method on your Listen class, which in turn calls srvThread.Stop()


  • Mitch Walker - MSFT

    Peter Ritchie wrote:
    Handling the form close/closi ng events in an ApplicationContext won't help much unless references to the thread objects are available in that scope.


    I concur with that wholeheartedly...I am just playing color man to your commentary and throwing out suggestions. <g>


  • Simon Dahlbacka

    For Abort() to work your thread method *has* to process the ThreadAbortException. If the thread is blocked on another call it can't process that exception.

    If you simply want your thread to terminate when you close your application you can simply set the threads IsBackground to true before calling Start(). The CLR will not let an application terminate if non-background threads are still running.

    Ideally you should implement a way to deterministically stop your threads.

  • xplosiv_1

    Sorry, I meant Abort.  Why isn't it working   Are you getting an error
  • Jorne

    But there is No Stop() method for thread!

    It has an Abort(), but it's not working here...

  • GuyFawkes

    This may be off base...but you may want to create a custom ApplicationContext for your program to run within....

    When the form(s) close or are being closed, one can capture that event and handle the worker threads from an outside context. If this advice does not sound off the mark, check out the multi-form example shown in the ApplicationContext example in MSDN Application.Run Method (ApplicationContext)


  • Daikoku

    no errors, but the thread still runing

  • KimberlyL

    How are you starting the thread
  • Maddinel

     OmegaMan wrote:
    This may be off base...but you may want to create a custom ApplicationContext for your program to run within....

    When the form(s) close or are being closed, one can capture that event and handle the worker threads from an outside context. If this advice does not sound off the mark, check out the multi-form example shown in the ApplicationContext example in MSDN Application.Run Method (ApplicationContext) 
    Handling the form close/closing events in an ApplicationContext won't help much unless references to the thread objects are available in that scope.  If the thread's can't handle being aborted or terminated, it won't do much good anyway.  Sounds like the thread objects are created as members of the Form class; in which case handling the form's closing/closed events would be easier.  Using an ApplicationContext would certainly be the way to go if more than one instance of a form was used though and thread objects where shared amongst them...

  • AdrianR

    I start in the worker class' constructor: (a create a new instance of Listen class in my form)

    class Listen
    {
    public Thread srvThread;

    public Listen()
    {

    try
    {
    //Starting the UDP Server thread.
    srvThread = new Thread(new ThreadStart(StartReceive));
    ssrvThread.Start();


    .
    .
    .
    }

  • anu_ooo

    I think it happens because of :
    srv.ReceiveFrom(....) is a blocking method....

  • Stop Thread and exit program