C# - App - Not terminating application until all forms are closed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxx233
    New Member
    • Nov 2007
    • 32

    C# - App - Not terminating application until all forms are closed?

    Hello!

    I have an app that starts up and has a starting form. One of the icons on this beginning Form opens a new form and lets the user give some information on a task they need to remember to do soon. Upon submitting this information, it takes the user back to the starting form, BUT also opens up another form to serve as a popup-reminder. This reminder is minimized to the system tray upon instantiation, and after 10 minutes (or the user clicking on the system-tray icon) it pops up and lets them give details about how they completed the thing it's reminding them to complete.

    My problem: After the user submits the information they need to remember and they get dropped back to the beginning point, instinct says "get rid of this window, I no longer need it", and they close that mainForm. Since my App's Main() says Application.Run (mainForm) - when they close the mainForm it terminates the whole program, including the popup reminder. What I *want* is for the program not to terminate unless all windows (including the minimized popup) are closed, OR for the popup to run as a seperate App so that closing the first app doesn't affect it. Any guidance on which is better, and perhaps a nudge in the right direction? All help is appreciated!

    Maxx

    ps - Telling Main() to do Application.Run (formPopupRemin der) after instantiating and show()ing what's currently the mainForm won't work - because they could need to do other work off that mainForm, and this would make it so that upon submitting the information in the popup-reminder, it would close the whole app. An inverse problem from what I have now ;)
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can attach an event handler to the closeing (exiting?) event for the main form.
    There you can attempt some smarts.
    Such as "if the user tried to close the window, but there is a popup still active, cancel the close and instead hide the form"
    That would mean you would have to do some other smarts on the popup window as well to make sure the program exits at correct time.

    Another choice would be to make the main window be the window that owns that system tray icon. And maintain when to popup the reminder window and such, keeping the reminder window dumb.
    That way when you close main window you can just hide it if needed and can handle all the events when the user clicks the systray or its time to be reminded, and know to close itself.

    Comment

    • maxx233
      New Member
      • Nov 2007
      • 32

      #3
      Great! On the FormClosing event of my mainForm I did:

      private void mainForm_FormCl osing(object sender, System.Componen tModel.CancelEv entArgs e)
      {
      if (singleton.inst ance.openRemind ers > 0)
      {
      e.Cancel = true;
      this.Hide();
      singleton.insta nce.mainClosed = true;
      }
      }


      And then on my PopupReminder form:

      singleton.insta nce.openReminde rs--;
      if ((singleton.ins tance.mainClose d == true) && (singleton.inst ance.openRemind ers < 1))
      {
      Application.Exi t();
      }


      Thanks a lot!!

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Nice job, your solution to that was actually a lot more simple then I had thought.
        I had visions of it being much more complex.


        One thing I should remind you of if you did not see it already, is the "type" of closing event (it's part of the arguments in the event handler). You should make sure to always close out when the system is trying to shutdown, otherwise it will refuse to shutdown because your program does not end.

        Comment

        • Chrace
          New Member
          • Feb 2008
          • 6

          #5
          Agreed, nice clean method though the Windows shutdown should be considered. You can check on the FormClosing event whether the system is shutting down (System.Environ ment.HasShutdow nStarted) and ignore your own e.Cancel.

          My initial thought was to make the reminder an independent program but your version is cleaner in that you keep the link and single process instance.

          Comment

          Working...