Console app to exit itself?

G

Guest

Hi,

I have a console app written in C# to kill a certain process. I don't want
the console to come to the foreground when run and I want it to exit itself
without waiting for the 'Press any key to continue' prompt at the end.

How can I run it so that it is hidden and once it has completed exits
quietly by itself?

Many thanks,

Ronnie
 
J

Jon Skeet [C# MVP]

Ronnie Smith said:
I have a console app written in C# to kill a certain process. I don't want
the console to come to the foreground when run and I want it to exit itself
without waiting for the 'Press any key to continue' prompt at the end.

How can I run it so that it is hidden and once it has completed exits
quietly by itself?

Just don't have a call to Console.ReadLine at the end of the code. I
believe the debugger (in some situations) adds the "Press any key to
continue" prompt.

However, avoiding having a console window is harder. Is there any
reason it has to be a console app, rather than a WinForms app which
happens never to display anything?
 
W

Willy Denoyette [MVP]

Ronnie Smith said:
Hi,

I have a console app written in C# to kill a certain process. I don't want
the console to come to the foreground when run and I want it to exit
itself
without waiting for the 'Press any key to continue' prompt at the end.

How can I run it so that it is hidden and once it has completed exits
quietly by itself?

Many thanks,

Ronnie

Set the ProcessStartInfo WindowStyle property to ProcessWindowStyle.Hidden
like this...

ProcessStartInfo startInfo = new ProcessStartInfo(".....");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

The "press any key to..." comes from the debugger windows isn't it?


Willy.
 
G

Guest

Hi Jon, no - there is no Console.ReadLine at the end of the code. I thought a
console would be simpler than a form. This is the code:

using System;
using System.Diagnostics;

namespace ConsoleKill
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

Process [] localByName = Process.GetProcessesByName("mplayer2");
foreach(Process p in localByName)
p.CloseMainWindow();

}
}
}
 
G

Guest

Hi Willy,

Thanks for your suggestion but it doesn't seem to make any difference.
The message actually appears in the console window after it has completed.
Even in the release version.

Ronnie
 
W

Willy Denoyette [MVP]

I know, but you run this from within VS (F5) isn't it? Try to run it from a
command prompt.

Willy.
 
G

Guest

Thanks Willy. I had thought that running it from VS but using <CTRL F5> (i.e.
no debug) would be the same as from the command line. But you are right. VS
must add the pause at the end because it is being run from within the
environment.
 
B

Beetlejuice

Ronnie said:
Thanks Willy. I had thought that running it from VS but using <CTRL F5> (i.e.
no debug) would be the same as from the command line. But you are right. VS
must add the pause at the end because it is being run from within the
environment.

CTRL+F5 always places a pause (in batch file terminology) at the end of
execution. Pressing the play button will not result in a pause, nor
will pressing F5 alone.
 

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