Having a service run as a console program.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mufasa

    #1

    Having a service run as a console program.

    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.


  • Jeroen Mostert

    #2
    Re: Having a service run as a console program.

    Mufasa wrote:
    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.Us erInteractive) {
    s.OnStart();
    Console.ReadLin e();
    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
    ServiceProcessI nstaller.

    --
    J.

    Comment

    Working...