I have a Windows Service running that is event driven and each event is handled by its own thread. Now the events are usually coming at a rate of around 10 per second. Last action of the event is to save data to a database.
If saving the data fails, because a connection cannot be established to the Database, I want the first thread that encounters this problem to start a new reconnecting Task running at some interval (e.g. every 30 seconds). Any following threads I wish to simply end. There should be only one reconnecting Task running at any time.
How do I code so that the following event threads safely know that there is already a reconnection Task running and end it's life? Maybe there is a good design pattern for this?
EDIT: Following FelixD's suggested links: Does cancelling a cancellation token trigger an exception in the Task?
If so, I could probably catch the exception, so it would save the incoming data in a file rather than commit it to the database. I have tried to search for this, but it is not clear to me what happens when a Task is cancelled.