• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

Preventing the "Stop" command on a Windows Service during a critical section of code. (C#/ASP.NET)

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.
0
tacf
Asked:
tacf
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
AaronReamsCommented:
Hi there,

You should enter/check your critical section in the OnStop() handler of your service.

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
     // Need to enter critical section here to make sure everything is finished
}
0
 
jacobhooverCommented:
Yes, a critical section should do it.  Have it locked by the thread that is charging then updating the DB and then unlocking it.  Then, on the OnStop function of the service, you need to try to acquire a lock on the same critical section.  This will protect the stop from occuring while in the middle of the process.  However, you may wish to impliment a more robust solution in which you would flag the account as an update in progress before you start incase your application (service) is killed via terminate process.  I would also then have a seperate table to log the CC transactions.

Just my 2 c.

Jake
0
 
AaronReamsCommented:
Also, keep this in mind.  If someone manually stops your service via the Services control panel applet, as long as you don't return from the OnStop() function (i.e. by blocking that thread and finishing up all your work in a separate thread), your service will actually continue to run indefinitely.  

However, one caveat, windows will display an error message saying your service didn't shut down in a timely fashion.  It displays this message after waiting ~ 2 minutes for OnStop to return.  
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cookreCommented:
If this weren't c#, I'd clear the SERVICE_ACCEPT_STOP flag in field dwControlsAccepted of the SERVICE_STATUS struct passed to the SetServiceStatus() API.  However, given that c# makes its own calls to SetServiceStatus() so you don't have to, I'd be surprised if your own clearing of the bit lasted very long.

But it may be worth a try to avoid the nasty messages in the event log.
0
 
Fahad MukhtarDistinguished EngineerCommented:
try
this.CanStop = false;
while you are in the CS
0
 
tacfAuthor Commented:
I had already tried altering the value of CanStop and it results in an exception. The value of CanStop cannot be changed once the service has started.

As for logging the transactions, I am logging them in a log file in C:\Windows\System32\Logfiles rather than using the db for this.

How do I actually create a critical section in C#? I remember back in my University days studying mutual exclusion semaphores and all the rest but I don't know how to implement such a thing.
0
 
tacfAuthor Commented:
OK, I think I've got it.

In my OnStart() method I create a Mutex object called this.mutex.

At the start of my critical section of code, I have:

Mutex.WaitAny(new WaitHandle[] {this.mutex});

At the end of the critical section of code, I have:

this.mutex.ReleaseMutex();

In the OnStop() method, I have:

Mutex.WaitAny(new WaitHandle[] {this.mutex});

From testing with writing entries to the log file, I've discovered that this works properly. (I put in a 15 second delay in the middle of the critical section of code and when I stop the service, it waits until that part is finished.
0
 
tacfAuthor Commented:
I just realized I could use:

this.mutex.WaitOne();

instead of:

Mutex.WaitAny(new WaitHandle[] {this.mutex});

0
 
jacobhooverCommented:
@tacf
  Any feedback/resolution/points distrobution?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now