Can foreground threads be aborted on application shutdown in C#.Net?
        Posted  
        
            by Cartman
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Cartman
        
        
        
        Published on 2010-05-24T17:09:40Z
        Indexed on 
            2010/05/24
            17:11 UTC
        
        
        Read the original article
        Hit count: 250
        
c#
I inherited a c# winforms app (.NET 2.0) which creates a thread upon startup in a singleton class. This thread handles messages from a messaging topic (Tibco EMS).
When the app is shutdown, the thread doesn't remain the the background? How is this possible? Is there something else happening that can abort this thread?
Code snippet:
public class Startup {
    public static void main(string [] args) {
        MySingletonClass.Instance.Init();
       // do other things below
       Application.Run(new MainForm());
    }
public class MySingletonClass {
// singleton code
//..
//..
private Thread t;  
public void Init() {
    t = new Thread(new ThreadStart(poll));
}
private void poll() {
    while(true} {
        // listen for messages and process but there is no break clause
        // exceptions are also handled within to ensure control doesnt go out
    }
}
Pls. don't write to tell me this is bad code. I know it and i was going to refactor it when i realised that the app actually shutdowns correctly inspite of this aberration. So i want to understand how this is possible.
Thanks for your help
© Stack Overflow or respective owner