I have a Windows Service that I'm in the process of writing in C# using Visual Studio .NET. It is used to process credit card payments for Premium Members of our website. There is one part of the process which is really critical:
Once the user's details have been processed, resulting in the credit card being charged, it is important that the database is updated to set the user's billing date to one month in the future. If this does not happen, the card will be charged again an hour later, the next time the timer fires its "elapsed" event.
I have already got lots of try/catch blocks in my code and other flags I use to handle the situation where an exception occurs between these two lines of code. However, I realized that if someone stops the service during this critical section, the same problem could arise.
This leads to my question...
How can I ensure that my service does not stop until the important piece of code has finished executing?
This is fairly urgent so I would really appreciate any help that you could offer. It's my first Windows Service although I'm fairly familiar with C# and ASP.NET.