C#, Application does not terminate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eflatunn
    New Member
    • May 2007
    • 9

    C#, Application does not terminate

    Hi all,

    my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
    There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
    There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegal CrossThreadCall s property of the form to false to avoid exceptions. Can the problem depend on this?
    The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Have you tried using the application unload event?

    Comment

    • george1106
      New Member
      • Oct 2006
      • 14

      #3
      Try defining the thread you are creating manually as a Background worker,
      when you define the thread

      thread t = new thread(new threadstart(fun ction));
      t.IsBackground = true;
      t.Start();

      check that.. I'm not shure the property is named IsBackGround, it says something like that.. but with that property activate that thread finishes when the main thread finish.

      I hopy it helps you :D

      enjoy your day :D

      Comment

      • eflatunn
        New Member
        • May 2007
        • 9

        #4
        Thank you george, but it is exactly what I do.
        It works on my computer. But a thread continues working when I test my application on another computer. There is no .NET framework on that computer. But I put the framework in setup project. May the problem depends on it ??

        Thank you kenobewan. But I don't see the point in using application unload event. Can yo explain it please?

        Comment

        • darkmannofear
          New Member
          • Apr 2007
          • 18

          #5
          if you want to force the program termination you can use Environment.Exi t(0); this will terminate all the working environmnent try it may help

          Comment

          • vanc
            Recognized Expert New Member
            • Mar 2007
            • 211

            #6
            this sounds a bit weird, try Application.Exi t(); on close button event then.

            Comment

            • uzbones
              New Member
              • May 2007
              • 2

              #7
              I think what the OP is trying to say is that if you click on the 'X' using the ControlBox or use 'ALT-F4' key combination to close the main form, then the Form will directly call Application.Exi t() without going through any other validation. It will not use the Form.Closed(), Form.Closing() or any other method on its way out and they are obsolete in .NET v3 anyway.

              Does anybody know if the function they are calling can be overwritten?

              (Also before anybody says 'Hide the ControlBox' you can still 'ALT-F4' even if it's hidden to get the same results, and setting the thread's IsBackground property doesn't work either.)

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                Originally posted by eflatunn
                Hi all,

                my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
                There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
                There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegal CrossThreadCall s property of the form to false to avoid exceptions. Can the problem depend on this?
                The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.

                Are you sure its the thread?

                Try this:


                [CODE=cpp]Application.App licationExit += new EventHandler(Ap plication_Appli cationExit);
                ...

                static void Application_App licationExit(ob ject sender, EventArgs e)
                {
                WorkerThread.Ab ort();
                }[/CODE]

                that should force the workerthread to do a hard close, so i wouldnt suggest doing it in production, but it will show you if the problem really is the thread...

                Comment

                • uzbones
                  New Member
                  • May 2007
                  • 2

                  #9
                  Yes the issue is that the threads are still running, or you want the 'X' method to use your 'Close' method that cleans stuff up. However stopping the application abruptly is a bad thing as it could have left things half done...

                  A bit more browsing has brought me to this:
                  Code:
                          public const int SC_CLOSE = 0xF060;
                          public const int WM_SYSCOMMAND = 0x0112;
                  
                          protected override void WndProc(ref System.Windows.Forms.Message m)
                          {
                              if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
                                  MyClose();  
                  // your method that cleans everything up and then runs
                  // System.Environment.Exit(0) which WILL close the threads forcefully if needed
                  
                              base.WndProc(ref m);
                          }
                  Last edited by PRR; May 18 '09, 05:31 AM. Reason: Please post code in [code] [/code] tags.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    I had that trouble with Threads too.
                    You gotta send the thread the Abort() call.
                    It was throw all kinds of ThreadAborting exceptions in the thread and with that you can determine inside the thread when to exit.

                    With a little bit of sneakiness I put it in the dispose method of the calling form usually

                    Comment

                    • Gremsy
                      New Member
                      • Aug 2008
                      • 1

                      #11
                      eflatunn, did you every find the solution to this problem? I'm having it too. I set all my threads to IsBackground = true, yet sometimes on a test machine the process doesn't close even though the applications form is closed.

                      Comment

                      • juniornet
                        New Member
                        • May 2009
                        • 2

                        #12
                        What's the best statement to close threaded application and release memory?

                        Originally posted by Plater
                        I had that trouble with Threads too.
                        You gotta send the thread the Abort() call.
                        It was throw all kinds of ThreadAborting exceptions in the thread and with that you can determine inside the thread when to exit.

                        With a little bit of sneakiness I put it in the dispose method of the calling form usually
                        Hi

                        I also have problems with my c# application which manages threads and sockets connections. I noticed that when I press close for running Application.Exi t(), the program keeps on memory.

                        So I decided to put

                        Code:
                        Process.GetCurrentProcess().Kill();
                        it seemed to work pretty well. After reading your note here, I've put

                        Code:
                        Thread.CurrentThread.Abort();
                        It seemed to work too. But I was wondering which approach is the best for closing my application and release memory??

                        Comment

                        • Bassem
                          Contributor
                          • Dec 2008
                          • 344

                          #13
                          I forced the same problem with threads when I was doing database applications.
                          What I did is, On the thread that uses the database queries I test the connection if close the thread is aborted, and add event handler Closing or Closed to close this connection.
                          As the previous tone, you could declare a global field (accessed by both main thread and the other one) let's say it'll be bool workAbort is set to true Closed event handler change it to false, and your other thread should test it before any action it does.
                          I think now you can control many threads as you want.

                          Thanks,
                          Bassem

                          Comment

                          • juniornet
                            New Member
                            • May 2009
                            • 2

                            #14
                            Aborting Thread

                            I also use database access but with Enterprise Library 4. I think it'll be good no to worry and let this framework take care of any thread issues. That's why I didn't create any custom event handlers for closing threads. I've never needed it.

                            I noticed yesterday that when I use

                            Code:
                            Thread.CurrentThread.Abort();
                            it doesn't caused me problem and problems in debug mode, but when I execute my .exe application file directly from the bin folder, it throws an exception about cannot abort thread. The most weird thing is that this command was nested within a try cath {} block!

                            So I left only the statement

                            Code:
                            Process.GetCurrentProcess().Kill();
                            on my form_Closing event. Now I can start and close the applications, and I've got no thread issues so far.

                            Comment

                            • timonline
                              New Member
                              • Apr 2010
                              • 1

                              #15
                              Originally posted by george1106
                              Try defining the thread you are creating manually as a Background worker,
                              when you define the thread

                              thread t = new thread(new threadstart(fun ction));
                              t.IsBackground = true;
                              t.Start();

                              check that.. I'm not shure the property is named IsBackGround, it says something like that.. but with that property activate that thread finishes when the main thread finish.

                              I hopy it helps you :D

                              enjoy your day :D
                              Thanks george1106

                              I tried your advice about the

                              thread t = new thread(new threadstart(fun ction));
                              t.IsBackground = true;
                              t.Start();

                              "IsBackgrou nd" is the right property name and it worked out great. As soon as i terminate the program, the thread terminates and disappears from the task manager.

                              Comment

                              Working...