views:

72

answers:

5

Hi to all.

Some time ago i Wrote a service with a timer that make an action every n minutes. All was right. But now, I want to write a service that waits for a signal, message or something from a gui application for doing his job.

I want me process to sleep pacefull (not in a infinite loop sniffing something) until my winforms application tell him "Hey, do things and give me a ring when the work is done"

could someone give me staring point?

Thanks.

A: 

If the windows service is on the same machine, you could stop and start the service? or call a webservice that stops/starts a service on a another machine?

If you did have a service that polls (or "sniffs") for something to do , this could be a very small and basic call to a database to check for something that will trigger the actual work?

Mark Redman
If I Stop and Start the service, how can my program knows when the work is done and if an error ocurred?I want to avoid polls. Of course, I can do it in this way and then write the results in a table, but I think it would be better and elegant some kind of direct communication. Something like events raised by one side and catched by the other side.
Jonathan
You could just initaite the start of the service. The Service could then stop itself when done?
Mark Redman
+3  A: 

You can use Windows Communication Foundation to allow your client to communicate with your service, send it requests, and register to receive progress events from existing running jobs.

Mark Byers
+3  A: 

You need to use interprocess communication (IPC). For C#, this usually means either .NET remoting -- on older versions of .NET -- or Windows Communication Foundation (WCF) -- on newer versions of .NET.

Essentially, the client application connects to an interface implemented by the service, and can then call methods on it, as if it was in the same process.

If this is too complicated, you could use a named event object, which the service waits on, and the client sets.

Roger Lipscombe
+1  A: 

Starting point:

Self-hosting WCF service

Murph
+1  A: 

Any of the IPC mechanisms qualify to get this done. If your needs a simple, just message passing, consider either a named pipe (NamedPipeServerStream) or a socket. If they are elaborate, consider Remoting over an IPC channel or WCF.

Personally, I like named pipes for this. Just make sure the pipe name is prefixed by "Global\" so it is visible from the interactive desktop session. Encrypt the messages if security is a concern. Spin up a background thread in your service that makes a blocking call on the pipe stream to implement the message handling.

Hans Passant