My app won't close programatically

V

vbMark

First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I can not
figure out how to make my program close. I've tried Application.Exit()
but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.
 
G

Guest

I apologize, I didn’t fully read your post before replying.

You said that you haven’t had success with Application.Exit(), where did you
put it? Within Close() ? Also, do you have any threads running whose
IsBackground property is not true?

Brendan
 
V

vbMark

I put it here:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Application.Exit();
}

This is a single threaded app.

Mark

I apologize, I didn’t fully read your post before replying.

You said that you haven’t had success with Application.Exit(), where
did you put it? Within Close() ? Also, do you have any threads
running whose IsBackground property is not true?

Brendan


vbMark said:
First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I can
not figure out how to make my program close. I've tried
Application.Exit() but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.vbmark.com
Get freeware, learn things, make money.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
G

Guest

You are seeing it return to Main but never return from Main... quite odd...
are you using any COM objects or other external references that may be tied
up?

If not... for another crazy test... try adding another Application.Exit(),
but this one immediately after the existing Application.Run().

Brendan


vbMark said:
I put it here:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Application.Exit();
}

This is a single threaded app.

Mark

I apologize, I didn’t fully read your post before replying.

You said that you haven’t had success with Application.Exit(), where
did you put it? Within Close() ? Also, do you have any threads
running whose IsBackground property is not true?

Brendan


vbMark said:
First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I can
not figure out how to make my program close. I've tried
Application.Exit() but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.vbmark.com
Get freeware, learn things, make money.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.vbmark.com
Get freeware, learn things, make money.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
W

Willy Denoyette [MVP]

vbMark said:
I put it here:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Application.Exit();
}

This is a single threaded app.


The frmMain constructor runs before the message queue gets created and the
message pump starts, that means that Application.Exit() makes no sense here
(Application.Exit() posts a message to the message queue!!!). Your
application shouldn't be a windows application in the first place has you
don't even need the form to be created.

Willy.
 
V

vbMark

Tried the second Application.Exit() and no change.

I am using a COM object
FAXCOMLib.FaxServer faxServer = new FAXCOMLib.FaxServerClass();


You are seeing it return to Main but never return from Main... quite
odd... are you using any COM objects or other external references that
may be tied up?

If not... for another crazy test... try adding another
Application.Exit(), but this one immediately after the existing
Application.Run().

Brendan


vbMark said:
I put it here:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Application.Exit();
}

This is a single threaded app.

Mark

I apologize, I didn’t fully read your post before replying.

You said that you haven’t had success with
Application.Exit(), where did you put it? Within Close() ? Also,
do you have any threads running whose IsBackground property is not
true?

Brendan


:

First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I
can not figure out how to make my program close. I've tried
Application.Exit() but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.vbmark.com
Get freeware, learn things, make money.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
http://www.vbmark.com
Get freeware, learn things, make money.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
J

Jason Newell

Mark,
Unless your form contains gui components that ProcessFaxes() depends
on, I would just make ProcessFaxes() a static function and call it
directly from your Main(). In addition to this, you can exclude
Application.Run() which will allow your program to exit after
ProcessFaxes is complete. Just a thought.

Jason
 
V

vbMark

See inline comments...

The frmMain constructor runs before the message queue gets created and
the message pump starts, that means that Application.Exit() makes no
sense here (Application.Exit() posts a message to the message
queue!!!).

Ok, so how do I solve my problem?

Your application shouldn't be a windows application in the
first place has you don't even need the form to be created.

If you pass in a command line argument the app runs in GUI mode instead
of automated mode.
 
V

vbMark

Hi,

I need to have the GUI part because if the user passes a command line
argument then the GUI is displayed without running the automated
processes.

Thanks though.

Mark,
Unless your form contains gui components that ProcessFaxes()
depends
on, I would just make ProcessFaxes() a static function and call it
directly from your Main(). In addition to this, you can exclude
Application.Run() which will allow your program to exit after
ProcessFaxes is complete. Just a thought.

Jason

First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I can
not figure out how to make my program close. I've tried
Application.Exit() but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.
 
V

vbMark

vbMark said:
First this runs:


[STAThread]
static void Main()
{
/////////////////////
/// Don't let app run more than one instance.
///
Process aProcess = Process.GetCurrentProcess();
string aProcName = aProcess.ProcessName;
if (Process.GetProcessesByName(aProcName).Length > 1) return;

Application.Run(new frmMain());
}


Then this runs:


public frmMain()
{
InitializeComponent();
ProcessFaxes();
Close();
}

Even after running the Close function the program stays open. I can
not figure out how to make my program close. I've tried
Application.Exit() but still no joy.

If I click the Exit button that I made the form closes fine:

Private void btnExit_Click(object sender, System.EventArgs e)
{Close();}

Please help!

Thank you.


I have found the answer.


System.Environment.Exit(1);
 
W

Willy Denoyette [MVP]

vbMark said:
Ok, tried it. No joy.
Are you sure ProcessFaxes ever returns? your handlers are not active as long
as the constructor runs.
Note that a Forms constructor is not the right place to start a long running
task, after all ProcessFaxes has nothing to do with the creation of a Forms
instance.

Willy.
 

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

Top