如何在asp.net服务器端,后台开一个计时器,每小时运行一次?
1 如何在asp.net服务器端,后台开一个计时器,每小时运行一次?
2 如何在客户端开个计时器,也是每小时运行一次?
给我一个例题代码看行吗?
谢了
------解决方案--------------------服务器端用
下面的示例创建一个 Timer,它在十秒钟后在控制台上显示“Hello World!”(世界你好!)。
[Visual Basic, C#, C++] 对于此示例,使用 System.Timers 命名空间。
[Visual Basic]
Public Class Timer2
Public Shared Sub Main()
' Create a new Timer with Interval set to 10 seconds.
Dim aTimer As New System.Timers.Timer(10000)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Only raise the event the first time Interval elapses.
aTimer.AutoReset = False
aTimer.Enabled = True
Console.WriteLine( "Press 'q ' to quit the sample. ")
While Console.Read() <> CInt( "q ")
End While
End Sub
' Specify what you want to happen when the event is raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine( "Hello World! ")
End Sub
End Class
[C#]
public class Timer2
{
public static void Main()
{
// Create a new Timer with Interval set to 10 seconds.
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
// Only raise the event the first time Interval elapses.
aTimer.AutoReset = false;
aTimer.Enabled = true;
Console.WriteLine( "Press \ 'q\ ' to quit the sample. ");
while(Console.Read()!= 'q ');
}
// Specify what you want to happen when the event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine( "Hello World! ");
}
}
自己改一下.
客户端用
javascript
function FunctionName()
{
要执行的东西
}
setInterval( "FunctionName() ",1000);
后面的时间为毫秒
------解决方案--------------------protected void Application_Start(Object sender, EventArgs e)
{
init();
System.Timers.Timer myTimer=new System.Timers.Timer(3000);
//关联事件
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true ;
myTimer.Enabled = true ;
}
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//获取当前时间
init();
}
private void init()
{
Application.Lock();
Application[ "TIMEDEMO "]= DateTime.Now.ToString();
Application.UnLock();
}
------解决方案--------------------设计上最好分开,像这种事情,单独做个服务部署在服务器上更加方便编程和以后的扩展.
------解决方案--------------------直接写一个window services放到系统服务里去