Web pages executing one at a time

4 posts, 0 answers
  1. Robert Strazzarino
    Robert Strazzarino avatar
    41 posts
    Member since:
    Jan 2006

    Posted 12 Jun 2006 Link to this post

    This is definitely a miscellaneous question and has nothing to do with RAD controls but everyone is so smart here I thought I'd ask it! :- )

    I have an .aspx web form in my website and I have it set up to be hit once per hour.  I have a special requirement, however.  There are tons of things being done inside the "Page_Load" event and I only want that code executing one at a time.  In other words, if the web form was hit twice at about the same time, I dont want the Page_Load executing twice at the same time. I want the first "Page_Load" to be completely done before the second "Page_Load" even starts.  How do I set this up?  The rest of the web app needs to be running at full speed at this time of course, I just am concerned with the Page_Load on this particular web form.  This probably has something to do with locking and threads but I can't find out how to do it.
  2. Alex Norcliffe
    Alex Norcliffe avatar
    73 posts
    Member since:
    Dec 2004

    Posted 13 Jun 2006 Link to this post

    Hi Robert

    You have two options - one of them complex to learn and guaranteed to work, and the other easy to setup but not 100% thread-safe.

    1) Look up the use of Mutexes. A Mutex is effectively an object that remains in the AppDomain (i.e. in terms of ASP.NET it will be in existence regardless of WebForm or Session). At the start of a function that needs to be thread safe, you create your Mutex with a constant name and then call MyMutex.Lock(). Follow this with your page logic, and at the end ensure you call MyMutex.UnLock(). MAKE SURE you put this in a Try-Catch-Finally block so it will unlock regardless of any exceptions thrown! The Lock function is deceptively simple: if the Mutex (remember it is a singleton object so exists system-wide) has already been Locked but not Unlocked, then the function refuses to return control to the calling routine until the Unlock is called by the original code. So a second request to your Page_Load will just queue at the MyMutex.Lock() call until the first request is done.

    2) Slightly simpler is to use a shared boolean variable in your Global.asax.vb or .cs file. You could add two shared functionis, Lock or Unlock, to mimic the behaviour of (1). Lock sets the boolean IsLocked to True, but if IsLocked was True to begin with it runs in a loop with Thread.Sleep called for say a second to ensure that you don't just hog CPU usage (running a flat loop would). As I said, this method is more messy.

    There is a third but this can prevent proper operation of your other pages if you make heavy use of the Application collection. The Application collectioni has Lock and Unlock functions which will do exactly as you are needing; however any other pages that rely on this methodology will also pause whilst your uber-page is running.

    Hope this helps!
    Alex
  3. Robert Strazzarino
    Robert Strazzarino avatar
    41 posts
    Member since:
    Jan 2006

    Posted 15 Jun 2006 Link to this post

    Alex,

    I posted in a few forums and your answer was by far the best! Here is my final solution, thanks again!:



    Old Code:

     //1. check if it hasn’t been updated in an hour

    //2. run code to update (takes about 20 seconds)

    //3. update database to current updated time

     New Code:

     Mutex m = new Mutex(false, "update");

    //searched the entire application for a mutex named “update”. False means do not try to gain access yet.

    try
    {

        m.WaitOne();
        //1. check it it hasn’t been updated in an hour

        //2. Run code to update (takes 20 seconds)

        //3. update database to current updated time

    }

               

    catch(Exception exc)

     {

       Util.sendErrorEmail(exc.ToString());

     }

    finally

     {

            m.ReleaseMutex();

      }
  4. Alex Norcliffe
    Alex Norcliffe avatar
    73 posts
    Member since:
    Dec 2004

    Posted 16 Jun 2006 Link to this post

    Hi Robert

    No worries - glad to help

    Alex
Back to Top