Communication between Windows Service and Desktop Application

26,210

Solution 1

Don't get scared away by the named pipes concept, WCF will take care of the nasty bits for you. You'll only have to do the configuration, it's simply another communication channel, instead of using HTTP for example. So you'll need to look into WCF.

Check out this tutorial.

Solution 2

In other words, what's the simplest and/or most effective way to communicate a basic DateTime from my service to my app?

You should use named pipe in this scenario, though the other options that you have involve MSMQ, webservices.

Share:
26,210

Related videos on Youtube

Kyle G.
Author by

Kyle G.

Updated on June 06, 2022

Comments

  • Kyle G.
    Kyle G. almost 2 years

    I know that similar questions have been asked before, but even after all my Googling I'm still completely lost.

    I've written a small Windows Service that does what my main application used to do in a background thread (it made sense to move it to a separate service for several reasons).

    When the background thread was running as a part of my application it would fire an event every time it finished working (updating a database, in this case) and I would use a timestamp from that event to grab the appropriate information to update the window. I don't think the specifics are relevant, but please let me know if you need more details.

    Now that I've got the database-filling stuff running as a service, the question becomes: How do I replace what used to be done by the event?

    In other words, what's the simplest and/or most effective way to communicate a basic DateTime from my service to my app?

    I've never done any IPC before but started reading up on it and got thoroughly confused. Named Pipes seem to be my best bet so far, but I've been unable to find anything that helps me understand their functionality... All I've found are poorly-commented chunks of code or "tutorials" that go way deeper than I need, without clearly covering the basics.

    So, without being able to establish a good base, I'm a little stuck. What's the best method to implement simple communication that does what my event used to do and where can I learn the basics of that method?

    EDIT:

    As always, everyone here rocks, thanks for all the quick responses. Gerrie's link turned out to be exactly what I needed (though I didn't know that's what I needed at the time).

    • David Heffernan
      David Heffernan over 10 years
      Named pipes are quite low level. Why not WCF?
    • Kyle G.
      Kyle G. over 10 years
      Simply that I couldn't find anything that really made it clear how to get started with basic WCF implementation. I'm certainly open to it.
    • Alex
      Alex over 10 years
      You could create a memory mapped file that your service writes the data to, and that is read by your application. That is pretty simple to accomplish.
    • Kyle G.
      Kyle G. over 10 years
      @Alex, wouldn't that mean constantly checking to see if the file has been updated? That seems like an extra step for no reason (unless I'm missing something).
    • David Heffernan
      David Heffernan over 10 years
      Memory mapped file is a dire approach for this. Don't contemplate that.
    • David Heffernan
      David Heffernan over 10 years
      As for WCF, there are millions of tutorials out there.
    • Kyle G.
      Kyle G. over 10 years
      @David, re: WCF tuts - That's what I expected! So far I haven't found anything truly "WCF beginner" but at first glance Gerrie Schneck's link appears to be what I was looking for.
    • Alex
      Alex over 10 years
      @KyleG. true, you will not be notified and you would have to "poll" to read changes.
  • Kyle G.
    Kyle G. over 10 years
    To which "other options" are you referring, specifically?
  • Kyle G.
    Kyle G. over 10 years
    When I first created my service I chose the "Windows Service" template rather that the "WCF Window Service" template... What kind of a difference would that make? Would starting over with a WCF template make life easier?
  • Ehsan
    Ehsan over 10 years
    @KyleG. i have mentioned those as well in my answer. MSMQ and webservices
  • Kyle G.
    Kyle G. over 10 years
    Oh, sorry, I totally misread. The caffeine hasn't hit the old noodle yet :)
  • Rob
    Rob over 9 years
    Basically you can have a WCF part of a service that is listening on a port and then have the app ping the port on a certain event (ie user clicks a button). This tutorial has been very helpful for me. codeproject.com/Tips/497123/…
  • GuidoG
    GuidoG almost 9 years
    I am at the same point as the OP now, but the link to tech.pro is down. Does anybody has another good link or a backup of this article ?
  • BanksySan
    BanksySan almost 9 years
    Gaaa. The link is broken. Any chance of an update?
  • Cosmin Ioniță
    Cosmin Ioniță over 6 years
    Great tutorial. Thank you for mentioning it!