Where instantiate System.Threading.Timer

Posted on 2010-08-18
Last Modified: 2012-05-10
Dear experts,

I want to use System.Threading.Timer. My question is, where should I instantiate it to start ticking? I probably want to do it only once, and not on each Page_Load().
I trued to do it like this:
    private readonly System.Threading.Timer tpsTimer = new System.Threading.Timer(CheckForPastDue, null, 10000, 5000);
on my page (rather, MasterPage), but it didn't work as the CheckForPastDue method is not static.
Question by:tantormedia
  • 3
  • 2
LVL 33

Expert Comment

ID: 33465403
What do you need a timer for?  You might have much better luck with the javascript "setTimeout" function.  Due to the stateless nature of web pages, I don't see how a server side time object will do anything meaningful for you.

Author Comment

ID: 33465846
I want the timer to call a method that checks if some items are past due, and if there are any, to send email notifications.
I cannot use PageMethod as it has to be static, which I don't want.
LVL 33

Accepted Solution

raterus earned 500 total points
ID: 33465967
In the past, when I have to do something like this, it's completely outside of  I'll write a console based .net program that runs the check/sends emails, and then enter a scheduled task on the server to have it run every as often as it's needed.

There are also workarounds using the cache object that calls a method as often as you'd like,

The only downside to the latter approach is that the application will never shutdown on the webserver, which just uses up more server resources.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33466526

Try it in Global.asax.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Threading;

namespace TestNet4
    public class Global : System.Web.HttpApplication
        void Application_Start(object sender, EventArgs e)
            private readonly System.Threading.Timer tpsTimer = new System.Threading.Timer(CheckForPastDue, null, 10000, 5000);

        void Application_End(object sender, EventArgs e)
            //  Code that runs on application shutdown


        void Application_Error(object sender, EventArgs e)
            // Code that runs when an unhandled error occurs


        void Session_Start(object sender, EventArgs e)
            // Code that runs when a new session is started


        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.




Open in new window


Author Comment

ID: 33467821

Thank you for your answer. But my manager just said it should not be connected to my application, but has to be a separate program.

Author Closing Comment

ID: 33467830
Thank you for your help. I will also go with a Console application using System.Threading.Timer.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question