我的 ASP.Net 应用程序中有一个通用 HTTP 处理程序 (*.ashx),它执行一些基本但耗时的计算,将进度语句打印到输出,以便让用户了解情况。执行这些计算涉及读取一些在使用它们时被处理程序锁定的数据文件,因此对处理程序的两次调用不要同时开始处理是很重要的。
为了实现这一点,我在缓存中添加了一个变量,指示计算正在进行中,如果另一个用户已经存在,这可以防止主应用程序将用户发送到此处理程序。在 Handler 本身中,它检查是否设置了 Cache 变量,如果设置了 Cache 值,则应将用户发送回主应用程序。但是,当我通过两次访问处理程序来测试它时,一个访问执行得很好,第二个访问坐在那里并且什么都不做,直到第一个在它运行时完成。将 IsReusable 设置为 true 没有任何区别。
有人知道为什么会这样吗?
下面的代码:
public class UpdateStats : IHttpHandler
{
private HttpContext _context;
public const String UpdateInProgressCacheKey = "FAHLeagueWebUpdateInProgress";
public void ProcessRequest(HttpContext context)
{
//Use a Cache variable to ensure we don't call multiple updates
Object inprogress = context.Cache[UpdateInProgressCacheKey];
if (inprogress != null)
{
//Already updating
context.Response.Redirect("Default.aspx");
}
else
{
//Set the Cache variable so we know an Update is happening
context.Cache.Insert(UpdateInProgressCacheKey, true, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
}
context.Response.Clear();
context.Response.ContentType = "text/html";
this._context = context;
context.Response.Write("<pre>Please wait while we Update our Statistics, you will be automatically redirected when this finishes...\n\n");
//Get the Stats
Statistics stats = new Statistics(context.Server);
//Subscribe to Update Progress Events
stats.UpdateProgress += this.HandleUpdateProgress;
//Update
String force = context.Request.QueryString["force"];
stats.UpdateStats((force != null));
//Remove the Cache variable
context.Cache.Remove(UpdateInProgressCacheKey);
context.Response.Write("</pre>");
context.Response.Write("<meta http-equiv=\"refresh\" content=\"0;URL=Default.aspx\" />");
context.Response.Write("<p>If you are not automatically redirected please click <a href=\"Default.aspx\">here</a></p>");
}
private void HandleUpdateProgress(String message)
{
this._context.Response.Write(message + "\n");
this._context.Response.Flush();
}
public bool IsReusable
{
get
{
return false;
}
}
}
编辑
从主应用程序的母版页添加了代码:
public partial class FAH : System.Web.UI.MasterPage
{
private Statistics _stats;
protected void Page_Init(object sender, EventArgs e)
{
this._stats = new Statistics(this.Server);
if (this._stats.StatsUpdateNeeded)
{
//If the Cache variable is set we're already updating
Object inprogress = Cache[UpdateStats.UpdateInProgressCacheKey];
if (inprogress != null) this.Response.Redirect("UpdateStats.ashx");
}
}
//etc...
}