Stopping the Service: What to do in Service Classes?

When a service is being stopped, the ServiceManager flags the IsStopRequested boolean field to true. In your code, for example, if you are looping a huge array, you have to control this flag so that the service can stop successfully without error.

Let's say that we have a collection.

var users = GetQueue();

In your code, you loop in this collection. Meanwhile, the service is being stopped. You may choose two of these methods: