views:

390

answers:

4

Hi all,

I wrote a windows service and a gui for it. Of course gui mainly depends on the service. Is there a way for gui to wait for the service? Sometimes I need to reload service config from the gui and restart the service.

I was thinking about 2 solutions: 1. using while and sleep to wait for service controller status to change (of course the simplest solution :P) 2. implementin INotifiPropertyChanged interface somewhere (this looks to complicated for this trivial problem).

I was wondering is there more elegant way of doing it? Is there an event that I am missing somewhere?

+1  A: 

Use a kernel Event object. When you start both apps, have them create or open a named Event object, then wait on it. The other can signal it, flipping the state thus allowing the other app to stop waiting and run.

gbjbaanb
lol I think you mis linked that url
Allen
It is a really nice plasma display though. :-)
T.E.D.
hrmph. But I *want* that plasma so much :) Fixed the link, cheers.
gbjbaanb
lol, go with a LCD, better darks ;)
Allen
+1  A: 

I'd probably spawn a seperate thead to simply poll and see when your service controller status has changed, when the change occurs kill this thread. Then simply re spawn the thread when you need to start re-poll

Darknight
+3  A: 

ServiceController has a method WaitForStatus where you pass it an argument of type ServiceControllerStatus. You can use it like this:

controller.WaitForStatus(ServiceControllerStatus.Running);
Tommy Carlier
only trouble with that is it tells you when the service is running, not when the service has initialised itself and it ready to do the stuff you want. It may be enough for the OP, it may not.
gbjbaanb
I'm not sure, but I think the status is ServiceControllerStatus.Running when the service IS fully initialized (after the OnStart-method has been called). Before that, it's ServiceControllerStatus.StartPending.
Tommy Carlier
This is exactly what I needed. Sorry for not noticing this method. Apart from that I drunk way to much caffe I was looking for an event :P (my bad). Just as an addition to the case - I needed to use Refresh() method to update service data. Otherwise it showed it as running even after Stop() (but it might be connected with windows 7 I'm using).
kyrisu
+1  A: 

Use an EventWaitHandle. Your GUI can wait on the WaitHandle and the service will set it which will trigger the GUI to continue on with what it was doing before it started waiting. No polling, no looping, no mess.

This great article on C# threading is probably a better resource for info on WaitHandles

Allen