Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Controlling Windows Service

Back to Forum: Tech Off
  • User profile image
    Larsenal

    So I'm getting ready to write my first serious Windows Service.

    So I start off by inheriting ServiceBase and overriding OnStart, OnStop, OnPause, OnContinue, and OnShutdown.  Fine.

    For the sake of example, let's say I'm creating a service to that monitors the network connection on my system. The app will monitor network uptime.  Now I'd love to create something similar to IIS manager to manage my service via a GUI.  I want to be able to reset the uptime counter, retrieve the current uptime and retrieve a list with details about the the last 50 state changes (network went up or down).

    So far, the best way to do this seems to be to write a TCP/IP listener that will accept commands.  My GUI would send commands such as "RESET", "UPTIME", and "LIST CHANGES."

    This seems a bit clunky.  Is there a better way?

    I can't imagine that this is how IIS is configured.  For instance, when you execute aspnet_regiis.exe, how does this communicate with the iis service?

    I've searched online for an example where someone created a GUI to control their service, but my efforts were unfruitful.  Help is much appreciated.

    -D

  • User profile image
    Yggdrasil

    [C]

    Larsenal wrote:
    I've searched online for an example where someone created a GUI to control their service, but my efforts were unfruitful.  Help is much appreciated.


    The IIS service might not be the best example - after all, you can always talk to it via HTTP. Smiley

    Back to the point, though - you can override the OnCustomCommand() method. It receives an int which tells it which custom command to execute (up to you to implement). It can't return any values or result codes, though.

    You call it by instantiating a ServiceController object for your service and calling ExecuteCommand() with the required flag.

    This won't help you with retrieving information, though. Just for sending operation commands to the service. If you want real method calls and retrieving data, you'll have to open some sort of interface to the outside world. In .NET1.1 you could open a TCP remoting port, execute a custom command that then saves the data to a file, or a large variety of other options that all seem overkill for the job.
    In .NET2.0, you can use remoting via the IPCChannel object for  much more efficient cross-process communication.

  • User profile image
    Larsenal

    Thanks.  I didn't know about the OnCustomCommand() method.  I'll look into the IPCChannel object.  Looks promising.

  • User profile image
    dotnetjunkie

    If you only need local communication with the service, the best and most performant way to do it is using Memory Mapped Files

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.