c# - thread suicide question -


i want kill thread when bad happen. what's suggestion thread suicide?

you should never "kill" thread. main program, can abort using thread.abort method, not recommended, , unnecessary.

your thread should nothing more method executing long loop. if "really bad" happens, return method , thread end.

this enough such loop:

private void mythreadloop() {    try     {         while (somecondition)         {             // lengthy operation         }    }    catch (exception ex)     {         log.error("something bad happened: ", ex);    } }  public void start()  {       thread t = new thread(mythreadloop);      t.start() } 

on other hand, if need main program kill thread (but not "suicide" named it), should signal thread want end, , not abort without knowing going on in background.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -