使用应用程序锁定和解锁来检查在线用户?

时间:2011-02-28 12:51:28

标签: asp.net

在Global.asax文件中,我创建了用于检查在线用户的代码,例如

  void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;

}
void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

在Aspx.cs

Response.Write(Application["OnlineUsers"].ToString());

这里我没有在线用户..但我想知道Application.Lock()和UnLock()的用途是什么;方法

1 个答案:

答案 0 :(得分:0)

根据MSDN(HttpApplicationState.Lock Method):当您需要同时更改多个值(比如交易,如银行操作存款/取款)以防止其他会话时,应该使用锁定/解锁从影响国家