I see your example on https://wisej.com/docs/2.2/html/BackgroundTasks.htm and I don’t know where should I put the code BackgroundService.Stop() when application stop.
Thanks for your help,
That sample is for a single shared thread. It’s started when the class is loaded and it’s share among all sessions. When to stop the thread is entirely your choice. If you don’t stop it, the IIS will terminate the thread when the application (the app pool) is unloaded.
If you want to stop the thread programmatically you have to decide when depending on your app. You could count the sessions listening to the broadcast event by adding a counter in add() and remove() (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/add) and start the service on the first subscriber and stop it when the last unsubscribes. Or you can keep it running for an X amount of time after the last subscriber is gone. Or you can start it immediately when the class is loaded (like the sample) and stop it depending on usage, or on subscribers count, etc…
Please login first to submit.