如题,求详解,最好是能有个例子说明.
解决方案 »
- 我不小心把自己发的帖子给删掉了 我现在怎么查看?请csdn高手帮忙
- 获得焦点选中文本
- 如何实现整个项目page重载?
- winform界面绘制速度很慢
- 调用有返回值的存储过程问题,百思不得其解,急!!
- c#怎么来获取网页的加载时间?
- 读取Excel数据到datagridview的数据精度问题
- 请问各位高手在c#中使用数组的问题
- 讨论:如何安全的处理数据库发问题。问题非常棘手,希望对MS SQL并发处理有独到见解的高手参加!
- Remoting的问题,在线等待
- 为什么CheckedListBox的item的长度被自动截取了啊?
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本?
Imports System
Imports System.TimersPublic Class Timer1 Private Shared aTimer As System.Timers.Timer Public Shared Sub Main()
' Normally, the timer is declared at the class level,
' so that it stays in scope as long as it is needed.
' If the timer is declared in a long-running method,
' KeepAlive must be used to prevent the JIT compiler
' from allowing aggressive garbage collection to occur
' before the method ends. (See end of method.)
'Dim aTimer As System.Timers.Timer ' Create a timer with a ten second interval.
aTimer = New System.Timers.Timer(10000) ' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000
aTimer.Enabled = True Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine() ' If the timer is declared in a long-running method, use
' KeepAlive to prevent garbage collection from occurring
' before the method ends.
'GC.KeepAlive(aTimer)
End Sub ' Specify what you want to happen when the Elapsed event is
' raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
End Sub
End Class' This code example produces output similar to the following:
'
'Press the Enter key to exit the program.
'The Elapsed event was raised at 5/20/2007 8:42:27 PM
'The Elapsed event was raised at 5/20/2007 8:42:29 PM
'The Elapsed event was raised at 5/20/2007 8:42:31 PM
'...
C# 复制代码
using System;
using System.Timers;public class Timer1
{
private static System.Timers.Timer aTimer; public static void Main()
{
// Normally, the timer is declared at the class level,
// so that it stays in scope as long as it is needed.
// If the timer is declared in a long-running method,
// KeepAlive must be used to prevent the JIT compiler
// from allowing aggressive garbage collection to occur
// before the method ends. (See end of method.)
//System.Timers.Timer aTimer; // Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000); // Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); // Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true; Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine(); // If the timer is declared in a long-running method, use
// KeepAlive to prevent garbage collection from occurring
// before the method ends.
//GC.KeepAlive(aTimer);
} // Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}/* This code example produces output similar to the following:Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
*/ 下面的代码示例实现简单的间隔计时器,该计时器每五秒钟发一次警报。当发生警报时,MessageBox 显示该警报已启动次数的计数,并询问用户计时器是否应继续运行。Visual Basic 复制代码
Public Class Class1
Private Shared myTimer As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False ' This is the method to run when the timer is raised.
Private Shared Sub TimerEventProcessor(myObject As Object, _
myEventArgs As EventArgs)
myTimer.Stop() ' Displays a message box asking whether to continue running the timer.
If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
' Restarts the timer and increments the counter.
alarmCounter += 1
myTimer.Enabled = True
Else
' Stops the timer.
exitFlag = True
End If
End Sub Public Shared Sub Main()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
AddHandler myTimer.Tick, AddressOf TimerEventProcessor ' Sets the timer interval to 5 seconds.
myTimer.Interval = 5000
myTimer.Start() ' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While End Sub End Class
C# 复制代码
public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false; // This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
myTimer.Stop(); // Displays a message box asking whether to continue running the timer.
if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
MessageBoxButtons.YesNo) == DialogResult.Yes) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
} public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start(); // Runs the timer, and raises the event.
while(exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}
System.Timers.Timer只有Elapsed事件它们是两个不同的Timer,但起到的效果是一样的