Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Application terminates when window gets closed

Application terminates when window gets closed

Scheduled Pinned Locked Moved C#
question
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MarioMARTIN

    Hi! I have something like this:

    public class MyApp: System.Windows.Application
    {
    public MyApp()
    {

    m\_Window = new Window();
    
    //loads of other stuff
    

    }

    Window m_Window;
    }

    and it gets instantiated from:

    class Program
    {
    [STAThread]
    static void Main()
    {
    MyApp app = new MyApp();
    app.Run();
    }
    }

    Every time someone says m_Window.Close() the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #5

    Hi, a normal window app has an Application.Run(new Form1()); in its static main method. The Run method will show the Form1 instance (and run a message loop to do that), and it will return when the form gets closed or Application.Exit() gets called. as an alternative you can call Application.Run(); without arguments; that one will start a message pump and return when Application.Exit() gets called. Hope this helps.

    Luc Pattyn [Forum Guidelines] [My Articles]


    this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


    M 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, a normal window app has an Application.Run(new Form1()); in its static main method. The Run method will show the Form1 instance (and run a message loop to do that), and it will return when the form gets closed or Application.Exit() gets called. as an alternative you can call Application.Run(); without arguments; that one will start a message pump and return when Application.Exit() gets called. Hope this helps.

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      M Offline
      M Offline
      MarioMARTIN
      wrote on last edited by
      #6

      "as an alternative you can call Application.Run(); without arguments;" yeah thanks, but I think taht is what I showed already in my first posting. Maybe I should try it verbally: 1.) I want an application that instantiates a class 2.) This class should open a window 3.) When the window gets closed the class should stay 'alive' (and therefore also the application must not get terminated) Mario M.

      L 1 Reply Last reply
      0
      • M MarioMARTIN

        Hi! I have something like this:

        public class MyApp: System.Windows.Application
        {
        public MyApp()
        {

        m\_Window = new Window();
        
        //loads of other stuff
        

        }

        Window m_Window;
        }

        and it gets instantiated from:

        class Program
        {
        [STAThread]
        static void Main()
        {
        MyApp app = new MyApp();
        app.Run();
        }
        }

        Every time someone says m_Window.Close() the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.

        M Offline
        M Offline
        MarioMARTIN
        wrote on last edited by
        #7

        Forget it, I'll take the "derive from Window" approach Thanks, Mario M.

        M 1 Reply Last reply
        0
        • M MarioMARTIN

          "as an alternative you can call Application.Run(); without arguments;" yeah thanks, but I think taht is what I showed already in my first posting. Maybe I should try it verbally: 1.) I want an application that instantiates a class 2.) This class should open a window 3.) When the window gets closed the class should stay 'alive' (and therefore also the application must not get terminated) Mario M.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #8

          Hi, one way of doing that is: - create a Windows app with a main form - hide the main form (it initially might have served as a splash screen) - create another window/form/whatever, maybe inside the main form's Load event - do whatever you want with that window/form/whatever, closing it does not affect the (invisible) mainform, hence the app continues (doing what I might ask). :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          1 Reply Last reply
          0
          • M MarioMARTIN

            Forget it, I'll take the "derive from Window" approach Thanks, Mario M.

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #9

            Hello,

            MarioMARTIN wrote:

            Forget it,

            No! why? It's interesting and might help somebody else if cleared up!

            MarioMARTIN wrote:

            I'll take the "derive from Window" approach

            Which, is what Luc pointed out, very well explaint, here![^] He got my '5' for that!

            All the best, Martin

            M 1 Reply Last reply
            0
            • M MarioMARTIN

              So what's your point? In the first case I derived it from 'Application' and in the second I derived it from 'Window'. But I don't want to derive it from 'Window' because it is no window.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #10

              The point is, learn the difference between window.close and window.hidden


              only two letters away from being an asset

              M 1 Reply Last reply
              0
              • M Martin 0

                Hello,

                MarioMARTIN wrote:

                Forget it,

                No! why? It's interesting and might help somebody else if cleared up!

                MarioMARTIN wrote:

                I'll take the "derive from Window" approach

                Which, is what Luc pointed out, very well explaint, here![^] He got my '5' for that!

                All the best, Martin

                M Offline
                M Offline
                MarioMARTIN
                wrote on last edited by
                #11

                Hi! "Which, is what Luc pointed out, very well explaint, here!" And what I already posted here Ciao Mario M.

                M 1 Reply Last reply
                0
                • N Not Active

                  The point is, learn the difference between window.close and window.hidden


                  only two letters away from being an asset

                  M Offline
                  M Offline
                  MarioMARTIN
                  wrote on last edited by
                  #12

                  How about looking at the two DIFFERENT code samples I posted? In the first snipped I create an App that has a window as member (only one window). When the window gets closed also the app terminates. And in the second sample I create a hidden window that has an additional window as member (two windows). I don't say: m_window.hidden! I hide the parent! But I think creating a hidden window just to make sure that the class itself survives when one of its members gets closed is quite ugly. So, please have a look at the code first, before you start getting wise! ;-) Mario M.

                  N 1 Reply Last reply
                  0
                  • M MarioMARTIN

                    Hi! I have something like this:

                    public class MyApp: System.Windows.Application
                    {
                    public MyApp()
                    {

                    m\_Window = new Window();
                    
                    //loads of other stuff
                    

                    }

                    Window m_Window;
                    }

                    and it gets instantiated from:

                    class Program
                    {
                    [STAThread]
                    static void Main()
                    {
                    MyApp app = new MyApp();
                    app.Run();
                    }
                    }

                    Every time someone says m_Window.Close() the whole application gets terminated ( = also 'app' gets terminated). But that is not what I want, I want only to close m_Window ( = 'app' should keep running). So, where did I go wrong? Thanks, Mario M.

                    M Offline
                    M Offline
                    MarioMARTIN
                    wrote on last edited by
                    #13

                    Problem solved:

                    [STAThread]
                    static void Main()
                    {
                    Application app = new Application();
                    app.ShutdownMode = ShutdownMode.OnExplicitShutdown; //that's the trick
                    MyApp win = new MyApp();
                    app.Run();
                    }

                    class MyApp
                    {
                      public MyApp()
                      {
                        m\_Win = new Window();
                        m\_Win.ShowDialog();
                      }
                    
                      Window m\_Win;
                    }
                    

                    }

                    1 Reply Last reply
                    0
                    • M MarioMARTIN

                      Hi! "Which, is what Luc pointed out, very well explaint, here!" And what I already posted here Ciao Mario M.

                      M Offline
                      M Offline
                      Martin 0
                      wrote on last edited by
                      #14

                      Hello,

                      MarioMARTIN wrote:

                      And what I already posted here

                      I know! And I think Luc did also read your post, but wanted to clear it up for you and others!

                      All the best, Martin

                      1 Reply Last reply
                      0
                      • M MarioMARTIN

                        How about looking at the two DIFFERENT code samples I posted? In the first snipped I create an App that has a window as member (only one window). When the window gets closed also the app terminates. And in the second sample I create a hidden window that has an additional window as member (two windows). I don't say: m_window.hidden! I hide the parent! But I think creating a hidden window just to make sure that the class itself survives when one of its members gets closed is quite ugly. So, please have a look at the code first, before you start getting wise! ;-) Mario M.

                        N Offline
                        N Offline
                        Not Active
                        wrote on last edited by
                        #15

                        MarioMARTIN wrote:

                        before you start getting wise!

                        Too late, already am.


                        only two letters away from being an asset

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups