-1

I'm a newbie in C#. I have a windows service. In this service, I run a thread. I want to stop the service immediately after the thread finish. Currently, my code is like:

while(true){
     if(isThreadFinished){
          MyService.Stop();
          break;
     }
}

It work, but I feel that using while(true) like that is a stupid way. So, I want to know if there is any other way to achieve my request.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
user2500561
  • 133
  • 1
  • 2
  • 14

3 Answers3

1
bool x=true;
while(x==true){
 if(isThreadFinished){
      x=false;
      MyService.Stop();
      break;
 }
}
Super Hornet
  • 2,839
  • 5
  • 27
  • 55
0

I don't know C#, but usually you would have an API thread.join() or similar which waits for the thread to finish.

If nothing else you can block on the same synchronised object.

In Java, there's also Object.wait() and Object.notify() that does the trick.

boky
  • 815
  • 5
  • 10
0

And Why don't use the delegate? You have a sample and the documentation here , in the official MSDN website...

You just call you're delegate method before the end of your Thread...

That's more proprely that a while loop... and more optimize if your thread during a long time...

Doc Roms
  • 3,288
  • 1
  • 20
  • 37