简体   繁体   中英

Locking a Web Form Thread in C#

My web host allows me to configure 'scheduled tasks' which can be used to schedule HTTP request to web pages for doing maintenance-type tasks. I use these scheduled tasks to perform daily calculations for my site.

The problem is that sometimes multiple HTTP requests are made almost simultaneously, resulting in calculations being logged more than once. The method which performs the calculation checks that the respective calculation hasn't been logged for the current day, but the HTTP requests are made so fast that one doesn't finish before another is called.

Here is what I currently have:

  public partial class CalculateFOOADP : System.Web.UI.Page

{ private System.Object lockThis = new System.Object();

protected void Page_Load(object sender, EventArgs e)
{
  lock (lockThis)
  {
    if (SportSetting.Football.CalculateADP)
    {
      PerformFOOADPCalculations(SportSetting.Football.TimespanInDays);
    }
  }
}

private void PerformFOOADPCalculations(int timespanInDays)
{
  string currentStatSeason = SportSeason.GetCurrentSportStatSeason(Globals.FOOString).SeasonCode;
  string currentSeason = FOO.CurrentSeason;

  /* we need to be sure that we're not double-logging ADP calculations */
  // Quarterback ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.QB.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.QB.ToString(), timespanInDays);
  }

  // Running Back ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.RB.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.RB.ToString(), timespanInDays);
  }

  // Wide Receiver ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.WR.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.WR.ToString(), timespanInDays);
  }

  // Tight End ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.TE.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.TE.ToString(), timespanInDays);
  }

  // Kicker ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.K.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.K.ToString(), timespanInDays);
  }

  // Defense ADP
  if (ADPCalculation.GetADPCalculationCount(currentSeason, Globals.FOOString, FOOPositionsOffense.DF.ToString(), DateTime.Now.Date) == 0)
  {
    ADPManager.CalculateADP(Globals.FOOString, currentStatSeason, FOOPositionsOffense.DF.ToString(), timespanInDays);
  }
}

}

Due to the fact that each request gets a new page instance, there is no way to block execution of the method across page requests. Unfortunately the best solution was to schedule another task to occur some time later which would delete duplicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM