0

I have a code only ASPX page wich does some calculations on database data. I have then configured my hosting to have a scheduled task which calls the page once a day at 6:00 o clock in the morning. However, because of the existence of an alias on the web address the page get called twice.

How can I synchronize the execution of the page load event to avoid that the two threads do the same thing twice?

The total execution of the thread is about 6/7 seconds.

Thanks for helping

Lorenzo
  • 29,081
  • 49
  • 125
  • 222

1 Answers1

1

My first recommendation would be to spend more effort to eliminate that redundant call. That said, it would still be a good idea to have the sync in place, since the page could possibly be called multiple times by an outside process anyway. I would recommend something similar to what I'd posted in Locking to load data to cache

An example for your case:

private static object MyDataLoadLock = new object();

public List<object> MyData
{
    get
    {
        return this.Cache["MyDatabaseCalculations"] as List<object>;
    }
    set
    {
        this.Cache["MyDatabaseCalculations"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (MyData == null)
    {
        lock (MyDataLoadLock)
        {
            if (MyData == null)
            {
                MyData = GetDatabaseCalculations();
            }
        }
    }
}

private List<object> GetDatabaseCalculations()
{
    System.Threading.Thread.Sleep(6500);
    return new List<object>();
}
Community
  • 1
  • 1
Mike Guthrie
  • 4,029
  • 2
  • 25
  • 48