2014-05-05 110 views
1

我的網絡主機允許我配置'計劃任務',它可以用來爲進行維護類型任務的網頁安排HTTP請求。我使用這些計劃任務來爲我的網站執行每日計算。在C#中鎖定Web窗體線程#

問題是,有時多個HTTP請求幾乎同時進行,導致計算被記錄多次。執行計算的方法會檢查相應的計算是否在當天沒有記錄,但是HTTP請求的速度太快以至於在請求另一個之前沒有完成。

這是我目前有:

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

{ 私人System.Object的lockThis =新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); 
    } 
} 

}

+3

每個請求都會獲得一個新的頁面實例。你需要一個靜態字段。但是,請注意多個應用程序域。 – SLaks

回答

0

由於這樣的事實,每個請求獲得一個新的頁面實例,有沒有辦法阻止整個頁面請求的方法的執行。不幸的是,最好的解決方案是安排另一個任務在一段時間後發生,這將刪除重複項。