0

As my question here: Stop program when thread finished?

I have a window service and an aspx page. In aspx page, I have to start the service. This service will run a thread, after thread finish, it will stop the service. After that, my aspx page have to show result to screen.

So, I have to: Check if service running - Start service - check if services stop - Print result to screen.

Currently, my code is like:

while(true){
    if(isServiceStop){
         MyService.Start();
         while(true){
              if(isServiceStop){
                   Print result;
                   break;
              }
         }
         break;
    }
}

This way, it will skyrocket my CPU_Usage, so, I want to know if there is any other way to achieve my request

Community
  • 1
  • 1
user2500561
  • 133
  • 1
  • 2
  • 14
  • Is this C# syntax? Which version? :D – Nayan Aug 14 '13 at 10:24
  • Why is inner while required in your opinion? – Nayan Aug 14 '13 at 10:25
  • Yes, it is C#, I only write some kind of example so please don't care about detail. Inner while required because after start the service, I also need to check if it stop then print result to screen – user2500561 Aug 14 '13 at 10:33
  • Then did you try separate thread? It is better to be used (but not with while) than running `while` in main thread. Also, you may want to look into async, await if you are using .NET 4.5 (http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx) – Nayan Aug 14 '13 at 10:55

2 Answers2

1

Create two EventWaitHandle objects to indicate the service's status:

private EventWaitHandle ServiceRunningEvent;
private EventWaitHandle ServiceStoppedEvent;

// in service startup
ServiceRunningEvent = new EventWaitHandle(False, EventResetMode.Manual, "RunningHandleName");
ServiceStoppedEvent = new EventWaitHandle(False, EventResetMode.Manual,

"ServiceStoppedEvent");

// Show service running
ServiceStoppedEvent.Reset();
ServiceRunningEvent.Set();

And when the service exits, have it flip the values:

ServiceRunningEvent.Reset();
ServiceStoppedEvent.Set();

In your ASP.NET application, you create the wait handles in the same way, but rather than setting their values, you wait on them. So:

// if service isn't running, start it and wait for it to signal that it's started.
if (!ServiceRunningEvent.WaitOne(0))
{
    // Start the service
    // and wait for it.
    ServiceRunningEvent.WaitOne();
}

// now wait for the service to signal that it's stopped

ServiceStoppedEvent.WaitOne();

I do wonder, however, why you'd want to start and stop a service so often. Why not just have the service running all the time, and send signals when you need it to do things?

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Thank you! I want to start and stop my service only because I need to know when it finish. I have to insert 20->100 millions rows to DB, when it finish, I must get number of row insert success, update success or fail and many other informations to show it to customer. So, I used stop and start service as a signal to find out the job is done or not, I also can try many other way but it all take me to use while(true) to check if the job is done – user2500561 Aug 14 '13 at 14:22
0

I found that service have method WaitForStatus, so I only need to use below code and it work perfectly:

Myservice.WaitForStatus(ServiceControllerStatus.Stopped);
user2500561
  • 133
  • 1
  • 2
  • 14