asp.net 中實現頁面每隔一分鐘刷新一次的效果

在Global.asax

需要回顧的知識點是 線程 和  文本文件的讀寫。

<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<script runat="server">
    string logpath;
    Thread thread;
    void writelog()
    {
        while (true)
        {
            StreamWriter sw = new StreamWriter(logpath, true, Encoding.UTF8);
            sw.WriteLine(thread.Name + ":" + DateTime.Now.ToString());
            sw.Close();
            Thread.CurrentThread.Join(1000 * 60);
        }

    }
    void Application_Start(object sender, EventArgs e)
    {
        // 在應用程序啓動時運行的代碼
  
        logpath = HttpContext.Current.Server.MapPath("log.txt");
        thread = new Thread(new ThreadStart(writelog));
        thread.Name = "寫入登陸日誌線程";
        thread.Start();
       

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  在應用程序關閉時運行的代碼
     

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // 在出現未處理的錯誤時運行的代碼

    }

    void Session_Start(object sender, EventArgs e)
    {
        // 在新會話啓動時運行的代碼

    }

    void Session_End(object sender, EventArgs e)
    {
        // 在會話結束時運行的代碼。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式設置爲
        // InProc 時,纔會引發 Session_End 事件。如果會話模式設置爲 StateServer
        // 或 SQLServer,則不會引發該事件。

    }
      
</script>

 

/*

   知識點回顧線程

 

*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章