【问题标题】:Running a timer on asp.net project在 asp.net 项目上运行计时器
【发布时间】:2017-05-03 06:18:30
【问题描述】:

我有项目,我有Global.asaxfile。 我有一些方法,例如 Application_Start 设置会话计数器,Session_Start 增加会话,Session_End 减少会话和 Application_End 打印会话计数器。

我想每小时打印一个计数器,以便我的代码:

    protected void Application_Start(object sender, EventArgs e)
    {
        Application["LiveSessionsCount"] = 0;

        Timer aTimer = new System.Timers.Timer(1 * 60 * 1000); //one hour in milliseconds
        aTimer.Elapsed += new ElapsedEventHandler(LogCounterForSessions);
    }

    private void LogCounterForSessions(object source, ElapsedEventArgs e)
    {
        Logger.ForceWrite("LogCounterForSessions: " + DateTime.Now.ToString("H:mm:ss") + " - session's count: " + (int)Application["LiveSessionsCount"]);
    }

我的问题是没有日志(所以我将时间减少到一分钟,但没有写入日志)。当应用程序结束时,日志工作。

有什么建议为什么它不起作用?

【问题讨论】:

  • 你开始计时了吗?
  • 你应该看看Quartz.net。
  • @Boney 可怜我...没有启用它:( 谢谢

标签: asp.net timer counter global-asax


【解决方案1】:

我需要从写作开始

aTimer.Enabled = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2010-10-03
    • 1970-01-01
    • 2021-12-07
    • 2018-04-02
    相关资源
    最近更新 更多