This is a migrated thread and some comments may be shown as answers.

Web pages executing one at a time

3 Answers 55 Views
Miscellaneous
This is a migrated thread and some comments may be shown as answers.
Robert Strazzarino
Top achievements
Rank 1
Robert Strazzarino asked on 12 Jun 2006, 09:25 PM
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.

3 Answers, 1 is accepted

Sort by
0
Alex Norcliffe
Top achievements
Rank 1
answered on 13 Jun 2006, 12:06 PM
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
0
Robert Strazzarino
Top achievements
Rank 1
answered on 16 Jun 2006, 01:33 AM
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();

  }
0
Alex Norcliffe
Top achievements
Rank 1
answered on 16 Jun 2006, 08:54 AM
Hi Robert

No worries - glad to help

Alex
Tags
Miscellaneous
Asked by
Robert Strazzarino
Top achievements
Rank 1
Answers by
Alex Norcliffe
Top achievements
Rank 1
Robert Strazzarino
Top achievements
Rank 1
Share this question
or