views:

147

answers:

1

Hi guys! Please help! I call a threading.timer from global.asax which invokes many methods each of which gets data from different services and writes it to files. My question is how do i make the methods to be invoked on a regular basis let's say 5 mins?

What i do is: in Global.asax I declare a timer

protected void Application_Start()
{
    TimerCallback timerDelegate = new TimerCallback(myMainMethod);
    Timer mytimer = new Timer(timerDelegate, null, 0, 300000);
    Application.Add("timer", mytimer);
}

the declaration of myMainMethod looks like this:

public static void myMainMethod(object obj)
{
    MyDelegateType d1 = new MyDelegateType(getandwriteServiceData1);
    d1.BeginInvoke(null, null);
    MyDelegateType d2 = new MyDelegateType(getandwriteServiceData2);
    d2.BeginInvoke(null, null);
 }

this approach works fine but it invokes myMainMethod every 5 mins. What I need is the method to be invoked 5 mins after all the data is retreaved and written to files on the server.

How do I do that?

A: 

You should recreate a timer just before the end of the "myMainMethod", and disable/remove the previous timer. I am not aware about C3 syntax

Something like:

protected void Application_Start()
{
    TimerCallback timerDelegate = new TimerCallback(myMainMethod);
    Timer mytimer = new Timer(timerDelegate, null, 0, 300000);
    Application.Add("timer", mytimer);
}


public static void myMainMethod(object obj)
{
    MyDelegateType d1 = new MyDelegateType(getandwriteServiceData1);
    d1.BeginInvoke(null, null);
    MyDelegateType d2 = new MyDelegateType(getandwriteServiceData2);
    d2.BeginInvoke(null, null);
    // find a way to pass mytimer as an argument of the myMainMethod
    // or use a global variable
    Application.Remove("timer", mytimer);
    Application.Add("timer", mytimer);
}
Moisei