Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Where instantiate System.Threading.Timer

Posted on 2010-08-18
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 33465967
In the past, when I have to do something like this, it's completely outside of asp.net.  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 asp.net cache object that calls a method as often as you'd like,

The only downside to the latter approach is that the asp.net application will never shutdown on the webserver, which just uses up more server resources.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 asp.net 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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