public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
private   static   System.Threading.Timer   timer;   
private   const   int   interval   =   1000   *   60   *   10;//检查在线用户的间隔时间    public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
if   (timer   ==   null)   
timer   =   new   System.Threading.Timer(new   System.Threading.TimerCallback(ScheduledWorkCallback),   
sender,   0,   interval);   
    
DataTable   userTable   =   new   DataTable();   
userTable.Columns.Add("UserID");//用户ID   
userTable.Columns.Add("UserName");//用户姓名   
userTable.Columns.Add("FirstRequestTime");//第一次请求的时间   
userTable.Columns.Add("LastRequestTime");//最后一次请求的时间   
userTable.Columns.Add("ClientIP");//   
userTable.Columns.Add("ClientName");//   
userTable.Columns.Add("ClientAgent");//   
//userTable.Columns.Add("LastRequestPath");//最后访问的页面   
    
userTable.PrimaryKey   =   new   DataColumn[]{userTable.Columns[0]};   
userTable.AcceptChanges();   
    
Application.Lock();   
Application["UserOnLine"]   =   userTable;
Application.UnLock();
}
 
protected void Session_Start(Object sender, EventArgs e)
{
Session["UserID"]="001";
Session["UserName"]="fengqi";
} protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ }
protected   void   Application_AcquireRequestState(Object   sender,   EventArgs   e)   
{   
HttpApplication   mApp   =   (HttpApplication)sender;   
if(mApp.Context.Session   ==   null)
return;   
if(mApp.Context.Session["UserID"]==null   )   
return;   
string   userID   =   mApp.Context.Session["UserID"].ToString();   
                            
DataTable   userTable   =   (DataTable)Application["UserOnLine"];   
DataRow   curRow   =   userTable.Rows.Find(new   object[]{userID});   
if(curRow   !=   null)   
{   
this.GetDataRowFromHttpApp(mApp,ref   curRow);   
}   
else   
{
DataRow   newRow   =   userTable.NewRow();
this.GetDataRowFromHttpApp(mApp,ref   newRow);
userTable.Rows.Add(newRow);
}
userTable.AcceptChanges();   
    
Application.Lock();   
Application["UserOnLine"]   =   userTable;   
Application.UnLock();   
    
}   
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{

} protected void Application_End(Object sender, EventArgs e)
{

}

#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.components = new System.ComponentModel.Container();
}
#endregion private   void   GetDataRowFromHttpApp(HttpApplication   mApp,ref   DataRow   mRow)   
{   
if(mApp.Context.Session   ==   null)  
return;   
if(mApp.Context.Session["UserID"]==null   ||   mApp.Context.Session["UserName"]==null)   
return;   
string   userID   =   mApp.Context.Session["UserID"].ToString();   
string   userName   =   mApp.Context.Session["UserName"].ToString();   
//string   requestPath   =   mApp.Request.Path;   
                            
if(mRow["UserID"].ToString().Length<1)   
{   
mRow["UserID"]             =   userID;
mRow["UserName"]         =   userName;
mRow["FirstRequestTime"]   =   System.DateTime.Now;
mRow["ClientIP"]         =   mApp.Context.Request.UserHostAddress;
mRow["ClientName"]     =   mApp.Context.Request.UserHostName;
mRow["ClientAgent"]   =   mApp.Context.Request.UserAgent;
}
    
mRow["LastRequestTime"]   =   System.DateTime.Now;;
//mRow["LastRequestPath"]   =   requestPath;    } private   void   ScheduledWorkCallback   (object   sender)
{
string   filter   =   "Convert(LastRequestTime,'System.DateTime')   <   Convert('"   +   System.DateTime.Now.AddSeconds(-interval/1000).ToString()   +   "','System.DateTime')";   
DataTable   userTable   =   (DataTable)Application["UserOnLine"];
DataRow[]   lineOutUsers   =   userTable.Select(filter);
for(int  i=0;i<lineOutUsers.Length;i++)
{
DataRow   curRow   =   lineOutUsers[i]; //保存到数据库
// XsStudio.Database   db   =   new   XsStudio.Database();
curRow.Delete();
}
userTable.AcceptChanges();
    
Application.Lock();
Application["UserOnLine"]   =   userTable;
string a=Application["UserOnLine"].ToString();
Application.UnLock();
}   有点不懂~~
怎么样获取退出系统的时间~~~

解决方案 »

  1.   

    只想知道关闭所有的页面时哪个事件可以知道当前的时间~~
    是不是这个protected void Application_End(Object sender, EventArgs e)
    {

    }
    这个里面~~
    请教高手~~
      

  2.   

    如果客户端用户不是明确提交了离线操作,服务器端就不可能知道该用户是否还在线。只能在超时后通过Session_End事件近似的判断一下。
      

  3.   

    还有一个方法,定义一个表:包括[ID]、[登录时间]、[最近验证时间]两项,Client登录时为其分配一个唯一ID并记录登录时间,然后在Client的页面上通过Javascript定时提交在线验证信息,同时等新该客户的最近验证时间字段。
    另外写一个系统检测程序,定时检验这个表,当最近验证事件和当前系统事件的差大于预先设定的特定值就认为该用户离线了。
    这么处理可能会比Session_End精确一些,但相应系统和网络负担也随之加大了。
      

  4.   

    菜鸟,问下,获取Application中的数据不是传址吗?后面还用赋值赋回去吗?
    要是现在还有别人登录,再取UserOnLine数据,然后再赋回去,这样不就产生了数据的丢失。
    应该把Application.Lock();提到位置1吧?楼主怎么认为?protected void Application_AcquireRequestState(Object sender, EventArgs e)

    .......
    //位置1吧
    DataTable userTable = (DataTable)Application["UserOnLine"]; 
    .......
    Application.Lock();//提到前面
    Application["UserOnLine"] = userTable;//去掉
    Application.UnLock();}