Solved

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

Posted on 2004-10-25
426 Views
Last Modified: 2010-04-17
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
Question by:tacf
    9 Comments
     
    LVL 8

    Accepted Solution

    by:
    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
     
    LVL 7

    Assisted Solution

    by:jacobhoover
    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
     
    LVL 8

    Expert Comment

    by:AaronReams
    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
     
    LVL 23

    Expert Comment

    by:cookre
    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
     
    LVL 19

    Expert Comment

    by:Desp
    try
    this.CanStop = false;
    while you are in the CS
    0
     
    LVL 1

    Author Comment

    by:tacf
    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
     
    LVL 1

    Author Comment

    by:tacf
    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
     
    LVL 1

    Author Comment

    by:tacf
    I just realized I could use:

    this.mutex.WaitOne();

    instead of:

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

    0
     
    LVL 7

    Expert Comment

    by:jacobhoover
    @tacf
      Any feedback/resolution/points distrobution?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now