Having a service run as a console program.

M

Mufasa

I used to work with somebody who wrote services that if you ran them from a
console prompt with a -console it would run writing things to the screen as
a normal console program. I'm sure he programmed this himself but I was
wondering if anybody could give me guidance on how to do that.

TIA - Jeff.
 
J

Jeroen Mostert

Mufasa said:
I used to work with somebody who wrote services that if you ran them from a
console prompt with a -console it would run writing things to the screen as
a normal console program. I'm sure he programmed this himself but I was
wondering if anybody could give me guidance on how to do that.
public static void Main() {
using (var s = new Service1()) {
if (Environment.UserInteractive) {
s.OnStart();
Console.ReadLine();
s.OnStop();
} else {
ServiceBase.Run(s);
}
}
}

Or something along those lines. You don't need a separate command-line
option, unless you *really* plan on running the application in a
non-interactive console window as well.

What's much more fun is combining this with a command-line parser to install
your service when somebody passes /i, for example (rather than requiring
them to trot out InstallUtil). Doing this requires some mucking around with
ServiceProcessInstaller.
 

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