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.Exit(); not working in the form constructor, why?

Application.Exit(); not working in the form constructor, why?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • A Offline
    A Offline
    AngryC
    wrote on last edited by
    #1

    Hello, I'm using Application.Exit(); in the form contructor to terminate the application if certain condition is met... but it's not working! Why? and what can I do? Thanks...

    N 1 Reply Last reply
    0
    • A AngryC

      Hello, I'm using Application.Exit(); in the form contructor to terminate the application if certain condition is met... but it's not working! Why? and what can I do? Thanks...

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line

      Application.Run(new MyForm());

      So actually it excutes more like this:

      MyForm temp = new MyForm()
      Application.Run(temp)
      //Then temp dies here

      Well, not exactly but I made it this way to illustrate that your constructor must return first before the Run() method actually gets called, and of course calling the Exit() method is useless if the Run() didn't get excuted first. Suggested solution: Simple; Make the Application.Exit() call in your Form_Load event if applicable.

      Regards:rose:

      A E 2 Replies Last reply
      0
      • N Nader Elshehabi

        Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line

        Application.Run(new MyForm());

        So actually it excutes more like this:

        MyForm temp = new MyForm()
        Application.Run(temp)
        //Then temp dies here

        Well, not exactly but I made it this way to illustrate that your constructor must return first before the Run() method actually gets called, and of course calling the Exit() method is useless if the Run() didn't get excuted first. Suggested solution: Simple; Make the Application.Exit() call in your Form_Load event if applicable.

        Regards:rose:

        A Offline
        A Offline
        AngryC
        wrote on last edited by
        #3

        Thanks, Nader.

        1 Reply Last reply
        0
        • N Nader Elshehabi

          Hello I believe you are making the famous scenario of checking a condition -maybe login or something- at the start of your application, if(false) => Exit(). Right? Well if you take a look ate Program.cs file where the main method exists, you'd see this line

          Application.Run(new MyForm());

          So actually it excutes more like this:

          MyForm temp = new MyForm()
          Application.Run(temp)
          //Then temp dies here

          Well, not exactly but I made it this way to illustrate that your constructor must return first before the Run() method actually gets called, and of course calling the Exit() method is useless if the Run() didn't get excuted first. Suggested solution: Simple; Make the Application.Exit() call in your Form_Load event if applicable.

          Regards:rose:

          E Offline
          E Offline
          EricT
          wrote on last edited by
          #4

          Bad answer -- you can't Application.Exit() in Form_Load either. Only solution is change of architecture: don't start the form if you don't want to display it

          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