Shaitan00
Shaitan00

Reputation: 323

How to get a Windows Service custom Status? [C++/C#]

I have a created a C# windows service (Serv.exe) which is responsible for performing various tasks at the request of a running application (A.exe), some of these can take long periods of time and I need a way to know the status of my requested operation (running on the service) from the calling application (A.exe).

Currently the way I have started to implement this is by using the ControlService(handle, command, status) to send a custom command to the Service to perform the task being requested, I am also using QueryServiceStatus(handle, status) to get the status of the service from the SCM - so the basic plumbing is there and working.

Now the problem I have is, after sending my ControlService Command (which works fine) the calling application (A.exe) keeps running and, at a certain point, it needs to know if the task it requested of the service is finished or not - therefore I am looking for a way to Query the Service to report a custom status, not the standard running, stopped, pending, paused state of the actual service but the status of the request I made using the ControlService() request.

Is this at all possible? Any help or hints would be immensily appreciated.

Thanks,

Upvotes: 0

Views: 1505

Answers (2)

Ed Power
Ed Power

Reputation: 8531

What's worked very well for me is to set up an external queue, such as MSMQ or a database table. These are easy to code, support bidirectional traffic between app and service, allow for multiple apps talking to one service even over the network, and allow you to shut down the app and start it later with the messages intact. Plus, it's easier to debug because you can examine the traffic, particularly if you're using a database and marking the messages as read instead of deleting them. SQLite via the System.Data.Sqlite wrapper is perfect for this when you don't want to use a heavyweight database or don't want to worry about whether MSMQ is installed or running.

Another alternative is to put your logic inside of a WCF service and communicate to it via HTTP or TCP.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564333

In the past, when I've had to handle more complex communication, I usually switch from QueryServiceStatus to having the service actually provide a means of communication via IPC.

Sockets and Pipes both work very well for this. The client and service can have pretty much an unlimited freedom in terms of what is communicated this way.

Upvotes: 1

Related Questions