C#: GUI to display realtime messages from Windows Service

13,844

Solution 1

What you can do is have the windows service have way of registering for an event (you can do this through using Windows Communication Foundation). When your error comes up, it fires that event, and your winforms app will be notified. It's called a duplex contract:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/0eb69998-0388-4731-913e-fb205528d374/

http://msdn.microsoft.com/en-us/library/ms731184.aspx

Actually the really cool thing is that you can have multiple applications listening this way too. So you can display it on screen, and have another application log it etc. without the two external apps knowing anything about each other.

Solution 2

I know this has already been mentioned, but use Windows Communication Foundation (WCF). Specifically, use the Publish-Subscribe Framework developed by Juval Lowy, author of Programming WCF Services. The details are described in this excellent MSDN article, and the source code is available for free at Lowy's website.

The neat thing about this framework is that it decouples the publisher, e.g., your Windows service, from any subscribers, e.g., your GUI. The publisher "publishes" events that are of interest to the Pub/Sub Service, which is always available. From the publisher's point of view, it doesn't matter if there are any subscribers or not. The Pub/Sub Service takes care of routing events to any and all registered subscribers. In this way, your Windows service publishes events as they occur, your GUI will subscribe/unsubscribe to the Pub/Sub Service when it loads/exits, and the Pub/Sub Service will notify your GUI as events occur.

I have used this setup in my project, and it works extremely well.

Solution 3

What you're describing is inter-process communication, which can get messy.

The easiest and most elegant, but probably least reactive, is to have the service write entries as small text files (or append to a log), and have your GUI use a FileSystemWatcher to detect new files or updates to the log file, and read the file. You have to ensure that the service opens the file for appending in a "shared" manner, allowing read-only access while it's writing. Otherwise, you'll block one process or the other, probably causing lost messages.

Processes can communicate through some built-in pipelines. if your service writes messages to its StandardOutput pipe, the GUI can remotely attach a listener and receive events when messages are written. This is probably the most elegant non-file way to do what you want. Research the Process class, especially the OutputDataReceived event. You'll have to go look for the process from your GUI by some uniquely identifying information, using GetProcess().

Solution 4

alt text

I've actually used the BitFactory Logger that has a socket logger that you can use for this purpose.

Share:
13,844
chezy525
Author by

chezy525

Updated on June 04, 2022

Comments

  • chezy525
    chezy525 almost 2 years

    I've written a C# windows service which can write messages to a custom EventLog or to any number of files. These messages are all marked with some priority (so, for example, only ERRORs and WARNINGs get stored in the EventLog, but if desired a lot more can be stored to a file).

    What I'd like to do now is create a GUI that can listen for these messages and display them in real-time. Allowing a user to watch the current messages (at whatever their desired priority level), without the need to store everything to a file. I assume this is a separate program with some form of hook into the service, but I'm unsure of where to start.

    This is my first real windows service, so I seem to be missing some keywords for finding out how to do this... Are there any code samples, tutorials, references, etc. for how to do something like this?

    UPDATE
    A lot of helpful answers, I love it when there's many ways to solve a problem! I think I'm going to implement a self-hosting WCF based solution. I'm still very light on the details as I'm trying to learn about WCF (I believe it will prove quite useful for me in other projects)... but thus far, I've found the videos here to be the most helpful as an intro how-to.

  • Tom W
    Tom W over 13 years
    Good answer, for using a platform-standard mechanism and for providing comprehensive documentation references.
  • Tergiver
    Tergiver over 13 years
    Personally I like pipes, but that's probably because I'm most familier with them. You can also use a named pipe rather than a standard pipe.
  • chezy525
    chezy525 over 13 years
    The file method was actually one idea I'd though of, but couldn't find a clean way of doing it. The FileSystemWatcher looks like a very good solution for that! I don't think Pipes provide a clean solution for me... If I understand correctly, the service will need to know about the GUI (to redirect it's StandardOutput) at startup, which I'm not sure it will... Is this correct, or am I missing something?
  • Tergiver
    Tergiver over 13 years
    Not if you use named pipes. You create a pipe with a globally unique name (usually something like "\\.\pipe\Company Name\Application Name\Pipe Name"). The service creates the pipe when it starts and the GUI app can connect to that pipe by name.