Application.Exit(); does not end app.

G

Guest

Here is the code I am having problem:


[STAThread()]
static void Main(string[] args)
{
bool isPar = false;
if ((args.Length == 1)) {
if ((args(0).ToUpper() == "MYPAR")) {
isPar = true;
}
}
if (isPar == true) {
myApp myForm = new myApp();
myForm.AppStartUp(true);
} else {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myApp());
}
}

private void AppStartUp(bool cmSwitch)
{
Application.Exit();
}

And I am running my application under windows task scheduler although it
passes through Application.Exit(), the applications does not end. What might
the problem be?
 
B

Barry Kelly

[snip]
And I am running my application under windows task scheduler although it
passes through Application.Exit(), the applications does not end. What might
the problem be?

You haven't provided enough information. A complete, compiling snippet
would be best. For example:

---8<---
using System;
using System.Windows.Forms;

class App
{
[STAThread()]
static void Main(string[] args)
{
bool isPar = false;
if ((args.Length == 1))
{
if ((args[0].ToUpper() == "MYPAR"))
{
isPar = true;
}
}
if (isPar == true)
{
Form myForm = new Form();
AppStartUp(true);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
}
}

static void AppStartUp(bool cmSwitch)
{
Application.Exit();
}
}
--->8---

This application, based on the source code you supplied, exits in both
cases (whether MYPAR was passed or not).

Note that Application.Run() starts a message loop, and doesn't return
until the message loop has terminated. Application.Exit() ultimately
causes this message loop to return, after giving all forms a chance to
close. If you need the application to quit *immediately*, without
notifying any forms of closing etc, you can call Environment.Exit().

-- Barry
 
G

Guest

If parameter is not supplied, AppStartUp is not called and the form is shown.
If there is a parameter. AppStartUp is called and application should exit
since Application.Run(new Form()); is not called. But it does not happen
that way.



Barry Kelly said:
[snip]
And I am running my application under windows task scheduler although it
passes through Application.Exit(), the applications does not end. What might
the problem be?

You haven't provided enough information. A complete, compiling snippet
would be best. For example:

---8<---
using System;
using System.Windows.Forms;

class App
{
[STAThread()]
static void Main(string[] args)
{
bool isPar = false;
if ((args.Length == 1))
{
if ((args[0].ToUpper() == "MYPAR"))
{
isPar = true;
}
}
if (isPar == true)
{
Form myForm = new Form();
AppStartUp(true);
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
}
}

static void AppStartUp(bool cmSwitch)
{
Application.Exit();
}
}
--->8---

This application, based on the source code you supplied, exits in both
cases (whether MYPAR was passed or not).

Note that Application.Run() starts a message loop, and doesn't return
until the message loop has terminated. Application.Exit() ultimately
causes this message loop to return, after giving all forms a chance to
close. If you need the application to quit *immediately*, without
notifying any forms of closing etc, you can call Environment.Exit().

-- Barry
 
B

Barry Kelly

JIM.H. said:
If parameter is not supplied, AppStartUp is not called and the form is shown.
If there is a parameter. AppStartUp is called and application should exit
since Application.Run(new Form()); is not called. But it does not happen
that way.

Did you try compiling and running the code I posted? If you supply the
MYPAR parameter, AppStartUp gets called, and the application *does*
exit.

How is your code different from the code I posted? Can you modify the
code I posted to reproduce your problem (and is still complete enough to
compile and run), and repost here?

-- Barry
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Are you using threads?
if (isPar == true) {
myApp myForm = new myApp();
myForm.AppStartUp(true);
} else {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myApp());
}
}

Not clear what your intentions are here.

Why are you creating a form (and not creating the message pump) in the IF
part?

if you want to end the app just do:

if (isPar != true) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myApp());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top