简体   繁体   English

在C#中锁定Web表单线程

[英]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. 我的Web主机允许我配置“计划任务”,该任务可用于计划HTTP请求到网页以执行维护型任务。 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. 问题在于,有时几乎同时发出多个HTTP请求,导致计算记录多次。 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. 执行计算的方法会检查当天尚未记录相应的计算,但是HTTP请求的执行速度如此之快,以至于一个请求在另一个调用之前没有完成。

Here is what I currently have: 这是我目前拥有的:

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

{ private System.Object lockThis = new System.Object(); {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. 不幸的是,最好的解决方案是安排另一个任务在一段时间后执行,这将删除重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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