C# 定时器间歇性停止

发布于 2024-11-19 14:32:56 字数 1051 浏览 4 评论 0原文

任何人都可以提出为什么 C# 计时器(在代码隐藏类中创建)会在没有被告知的情况下停止的任何原因吗?

我的计时器在页面加载时启动,然后在单击按钮时停止。我不需要单击按钮,它有时会停止。据我所知,IIS 没有重新启动,也没有抛出任何错误。

这让我很困惑......

谢谢。

    // This gets called on page_load
    private void checkTimer()
    {
        if (!parentTimer.Enabled) // If parent timer is not enabled then it is probably the start of a new day (after a night time server backup which kills timers)
        {
            parentTimer.Interval = 60000; // One minute
            parentTimer.Elapsed += new ElapsedEventHandler(parentTimer_Elapsed); // Define what happens when elapsed
            parentTimer.AutoReset = true; // Set timer to repeat
            parentTimer.Enabled = true; // Start the timer
        }
    }

    protected void btnCancel_Click(object sender, System.EventArgs e)
    {
        parentTimer.Stop();
        ...etc...
    }

注意:我根本没有更改 ParentTimer 的 elapsed 方法。

基本上,ParentTimer 管理着 ChildTimer 列表。如果 ParentTimer 过去了,它会检查一个或多个 ChildTimer 是否也过去了,如果是,则有一个事件,如果没有,则重置 ChildTimer 并继续。

Can anybody suggest any reasons why a C# timer (created in a code-behind class) would stop without being told to?

My timer starts on page load and then stops when I click a button. I don't need to click the button for it to sometimes stop. IIS is not being restarted to my knowledge and no errors are being thrown either.

This is confusing me quite a bit...

Thanks.

    // This gets called on page_load
    private void checkTimer()
    {
        if (!parentTimer.Enabled) // If parent timer is not enabled then it is probably the start of a new day (after a night time server backup which kills timers)
        {
            parentTimer.Interval = 60000; // One minute
            parentTimer.Elapsed += new ElapsedEventHandler(parentTimer_Elapsed); // Define what happens when elapsed
            parentTimer.AutoReset = true; // Set timer to repeat
            parentTimer.Enabled = true; // Start the timer
        }
    }

    protected void btnCancel_Click(object sender, System.EventArgs e)
    {
        parentTimer.Stop();
        ...etc...
    }

Note: I do not change ParentTimer at all in its elapsed method.

Basically ParentTimer governs a list of ChildTimers. If ParentTimer elapses it checks if one or more of the ChildTimers have elapsed too, if so, there is an event, if not then it resets the ChildTimer and carries on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-11-26 14:32:56

我怀疑这是因为页面的工作进程在请求结束时停止。

您可以尝试增加请求超时,但更好的问题是您能解释一下为什么要这样做吗?您想要解决的问题是什么?

请记住,无论 ASP.Net 在代码中添加了多少让您感觉舒服的内容(会话状态、视图状态等),Web 请求都是无状态的,应该被视为一个独特的逻辑传递,它与 Windows 不同应用程序,其中 void Main(...) 函数中的后台代码线程不断运行。

My suspicion is it's because the worker process for the page is stopping at the end of the request.

You could try increasing the request time out, but a better question is Can you explain why you're trying to do this ? What is the problem you're trying to solve ?

Remember, that regardless of all the fluff that ASP.Net puts around your code to make you feel comfortable (session state, viewstate etc), a web request is stateless and should be considered as a distinct pass of logic, it's not like a windows application where a background thread of code in your void Main(...) function is constantly running.

樱花细雨 2024-11-26 14:32:56

计时器与创建它的线程相关联,在 ASP.net 的情况下,处理给定用户发出的每个页面请求的线程将由于工作线程和线程池的使用而频繁更改。

在页面级别使用计时器根本行不通;您需要跟踪会话级别的状态(与特定用户相关)作为起点。

事实上,我根本不会在 Web 应用程序中使用计时器,因为它们的执行根本无法得到保证。

如果您使用它来运行后台任务 - 考虑在 Application_Start 或类似的东西中启动您自己的工作线程。当应用程序池回收时,该线程将被终止。您还应该考虑手动关闭线程,应用程序也将被关闭。

但请注意这一点,该线程不能假设它始终是唯一运行的线程 - 由于 IIS 重叠回收,当新线程启动时,旧线程仍可能在旧 AppDomain 中运行。

A timer is tied to the thread that created it and in the case of ASP.net the thread that handles each page request issued by a given user will change frequently due to the use of worker threads and the thread pool.

Using a timer at page-level simply won't work; you need to be tracking the state at Session-level (tied to the particular user) as your starting point.

In fact, I just wouldn't use timers at all in a web application, because their execution is simply not guaranteed.

If you're using this to run a background task - consider firing up your own worker thread in Application_Start or something like that. The thread will be terminated when the app pool recycles. You should also look at manually shutting the thread down the application is being shut down too.

Be careful with this, however, this thread can't assume it's always the only one running - due to IIS overlapped recycling, when a new one fires up the old one could still be running in the old AppDomain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文