Results 1 to 5 of 5

Thread: Ending application with busy threads

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Angry Ending application with busy threads

    Hi,
    having WindowsForm application, if I do Application.Exit() and some threads are running, the main GUI terminates, but the VS IDE stays in run mode keeping these threads running.

    How to properly exit my application if it hase more threads? I can't find way to exit or terminate the thread, especially if it is busy with external code (eg. blocked by Socket blocking methods).
    Thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Can't you just use terminate it with Abort method of the running thread . You'd probably need to declare this public in a class so you can have a reference to it .

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well, in the documentation about Abort() method is written: Calling this method usually terminates the thread.

    It only queues the request to terminate, which you can even cancel by calling Thread.ResetAbort().
    And if the thread is blocked waiting for response, this does not work.


    ---
    PS: If I terminate the thread, do all it's resources become free?

  4. #4
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    How about using. Application.ExitThread();
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Nope, this does not work.

    And in MSDN, about Application.Exit() is written:
    This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.
    So this is just what I don't need, I need all threads to terminate. Just try it!

    PHP Code:
    using System.Net;
    using System.Threading;


        static 
    Thread MyThread;
        static 
    Socket MySocket;

        [
    STAThread] static void Main(string[] args)
        {
            
    MyThread = new Thread(new ThreadStart(MyThreadProc));
            
    MyThread.Start();

            
    // Application.Run(new MyForm());
        
    }

        public static 
    void MyThreadProc()
        {
            
    TcpListener MyListener = new TcpListener(IPAddress.Parse("127.0.0.1"),10000);
            
    MyListener.Start();
        }

        
    // private static void MyForm_Load(object sender, EventArgs e)
        // {
        //     Application.Exit();
        // } 
    As you can see, I only exit the message loop for the loaded form, which continues the code after Application.Run(). This exits [STAThread] and returns to IDE, but it's still in run mode and I need to click stop button in order to really exit from my app.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width