19

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).

Kyle G.
  • 870
  • 2
  • 10
  • 22
  • 1
    Named pipes are quite low level. Why not WCF? – David Heffernan Aug 30 '13 at 14:58
  • 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. – Kyle G. Aug 30 '13 at 15:01
  • 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. – Alex Aug 30 '13 at 15:03
  • 1
    @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). – Kyle G. Aug 30 '13 at 15:04
  • Memory mapped file is a dire approach for this. Don't contemplate that. – David Heffernan Aug 30 '13 at 15:05
  • As for WCF, there are millions of tutorials out there. – David Heffernan Aug 30 '13 at 15:05
  • @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. – Kyle G. Aug 30 '13 at 15:06
  • @KyleG. true, you will not be notified and you would have to "poll" to read changes. – Alex Aug 30 '13 at 15:29

2 Answers2

8

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • 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? – Kyle G. Aug 30 '13 at 15:02
  • 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. http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp – Rob Nov 17 '14 at 17:47
  • 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 ? – GuidoG Jul 13 '15 at 09:17
  • Gaaa. The link is broken. Any chance of an update? – BanksySan Aug 08 '15 at 20:28
  • Great tutorial. Thank you for mentioning it! – Cosmin Ioniță Sep 26 '17 at 11:19
4

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.

Ehsan
  • 31,833
  • 6
  • 56
  • 65