2009-11-29 38 views
0

我从global.asax调用一个threading.timer,它调用许多方法,每个方法都从不同的服务获取数据并将其写入文件。 我的问题是我如何让方法被定期调用让我们说5分钟?Threading.Timer异步调用很多方法

我要做的就是: 在Global.asax中,我宣布一个计时器

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

myMainMethod的声明如下所示:

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

这种方式工作得很好,但它调用myMainMethod每5分钟。我需要的是在所有数据被重新分区并写入服务器上的文件5分钟后调用该方法。

我该怎么做?

回答

0

您应该在“myMainMethod”, 结束之前重新创建一个计时器,并禁用/删除前一个计时器。我不知道关于C3语法

喜欢的东西:

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); 
}