Solved

Where instantiate System.Threading.Timer

Posted on 2010-08-18
6
431 Views
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.
Thanks.
0
Comment
Question by:tantormedia
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:raterus
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.
0
 

Author Comment

by:tantormedia
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.
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 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,
http://www.beansoftware.com/ASP.NET-Tutorials/Scheduled-Tasks.aspx

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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33466526
Hi,

Try it in Global.asax.

Regards,
VSS

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

0
 

Author Comment

by:tantormedia
ID: 33467821
vs00saini,

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.
0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now