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. Terminating an Application ?

Terminating an Application ?

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 2 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 Offline
    M Offline
    Mike Bluett
    wrote on last edited by
    #1

    Talking about the designers of C# making something complicated or what! I have built a LogViewer application and my objective is to terminate the application if it determines there are no log files to display or if the logfile is in use by another application. I run the application from Main as follows: Application.Run(new MainForm()); In MainForm, it first does InitializeComponent, then it Populates the LogSelector with some user choices and then it adds logging info to the appropriate control if a log file is available (DisplayLogInfo). If DisplayLogInfo detects that no logfile is available or the logfile is being used by another app, I want LogViewer to not show any form and to terminate. I have tried this.Hide(). I have tried Application.Exit(). I have tried to make the form invisible by setting this.Visible to false. I have tried this.Dispose(), but then I get an unhandled exception and would rather not handle the exception by ignoring it. Regardless, the form always displays and I have to close the app via closing the Window by the Close button. Can anyone suggest a way to get this to work. I would rather not rewrite the app. so that it checks for the logfile before running InitializeComponent as I believe there must be a way to accomplish the task as I have written it and I would like to learn how it can be done. Comments?

    L 1 Reply Last reply
    0
    • M Mike Bluett

      Talking about the designers of C# making something complicated or what! I have built a LogViewer application and my objective is to terminate the application if it determines there are no log files to display or if the logfile is in use by another application. I run the application from Main as follows: Application.Run(new MainForm()); In MainForm, it first does InitializeComponent, then it Populates the LogSelector with some user choices and then it adds logging info to the appropriate control if a log file is available (DisplayLogInfo). If DisplayLogInfo detects that no logfile is available or the logfile is being used by another app, I want LogViewer to not show any form and to terminate. I have tried this.Hide(). I have tried Application.Exit(). I have tried to make the form invisible by setting this.Visible to false. I have tried this.Dispose(), but then I get an unhandled exception and would rather not handle the exception by ignoring it. Regardless, the form always displays and I have to close the app via closing the Window by the Close button. Can anyone suggest a way to get this to work. I would rather not rewrite the app. so that it checks for the logfile before running InitializeComponent as I believe there must be a way to accomplish the task as I have written it and I would like to learn how it can be done. Comments?

      L Offline
      L Offline
      Lisa Jorgensen
      wrote on last edited by
      #2

      Add a handler for MainForm's Load event, and execute this.Close() inside that handler. E.g.:

      private void MainForm_Load(object sender, System.EventArgs e)
      {
      // Insert code to determine whether logfile is
      // available.

      if (LogFileIsNotAvailable)
      {
          this.Close();
      }
      

      }

      M 1 Reply Last reply
      0
      • L Lisa Jorgensen

        Add a handler for MainForm's Load event, and execute this.Close() inside that handler. E.g.:

        private void MainForm_Load(object sender, System.EventArgs e)
        {
        // Insert code to determine whether logfile is
        // available.

        if (LogFileIsNotAvailable)
        {
            this.Close();
        }
        

        }

        M Offline
        M Offline
        Mike Bluett
        wrote on last edited by
        #3

        I see that your suggestion should work; however, I am still a little puzzled. Can you tell me the reason why the "this.Hide()" or "this.Close()" did not work when implemented within the MainForm constructor?

        L 1 Reply Last reply
        0
        • M Mike Bluett

          I see that your suggestion should work; however, I am still a little puzzled. Can you tell me the reason why the "this.Hide()" or "this.Close()" did not work when implemented within the MainForm constructor?

          L Offline
          L Offline
          Lisa Jorgensen
          wrote on last edited by
          #4

          First, I gave you bad (or at least incomplete) advice. I meant to say to use the Application.Idle event rather than the Load event. Calling Close from the Load event can cause a memory leak. You could do something like:

          public partial class MainForm : Form
          {
          public MainForm()
          {
          InitializeComponent();

              // Code to determine whether to close form   
              
              if (ShouldClose)
              {
                  Application.Idle += new EventHandler(Application\_Idle);
              }
          }   
          
          
          void Application\_Idle(object sender, EventArgs e)
          {
              this.close();
          }
          

          }

          The Hide and Close methods don't work in the constructor because the Form is still being constructed. And in any case you don't want to close the form at this point, assuming the calling code looks something like:

          Application.Run(new MainForm());

          If MainForm is closed and disposed before it is constructed, then Application.Run will throw an ObjectDisposedException.

          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