在 C# 中使用计时器进行定期检查事件?
using the timer in C# for periodic check events?
亲爱的兄弟们,我是新手,我正在使用 XML 作为数据库,我想使用 C# 中的 Timer 控件检索每 10 秒将数据提取到我的标签控件的方法。
For Instance 检索数据和定时器控件的方法是:
private void timer1_Tick(object sender, EventArgs e)
{
//here how can I run this method at every 10 seconds
ReturnUknowns();
}
如果您在创建计时器时遇到问题。要每 10 秒执行一次此功能,您需要在创建计时器时指定滴答周期。
Timer timer = new Timer(10000); // 10 seconds in miliseconds
timer.Tick += timer1_Tick;
以及您需要设置的地方
timer.Start()
private void timer1_Tick(object sender, EventArgs e)
{
//here how can I run this method at every 10 seconds
ReturnUknowns();
}
然后每 10 秒将调用函数 timer1_Tick() 并在其中指定所有函数。 timer.Tick 可能有不同的名称,具体取决于您所从事的项目类型。
这是您可以测试从 2 秒间隔中删除注释的代码,理解起来很有意义...
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. You can experiment with this
// by commenting out the class-level declaration and
// uncommenting the declaration below; then uncomment
// the GC.KeepAlive(aTimer) at the end of the 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);
}
}
亲爱的兄弟们,我是新手,我正在使用 XML 作为数据库,我想使用 C# 中的 Timer 控件检索每 10 秒将数据提取到我的标签控件的方法。
For Instance 检索数据和定时器控件的方法是:
private void timer1_Tick(object sender, EventArgs e)
{
//here how can I run this method at every 10 seconds
ReturnUknowns();
}
如果您在创建计时器时遇到问题。要每 10 秒执行一次此功能,您需要在创建计时器时指定滴答周期。
Timer timer = new Timer(10000); // 10 seconds in miliseconds
timer.Tick += timer1_Tick;
以及您需要设置的地方
timer.Start()
private void timer1_Tick(object sender, EventArgs e)
{
//here how can I run this method at every 10 seconds
ReturnUknowns();
}
然后每 10 秒将调用函数 timer1_Tick() 并在其中指定所有函数。 timer.Tick 可能有不同的名称,具体取决于您所从事的项目类型。
这是您可以测试从 2 秒间隔中删除注释的代码,理解起来很有意义...
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. You can experiment with this
// by commenting out the class-level declaration and
// uncommenting the declaration below; then uncomment
// the GC.KeepAlive(aTimer) at the end of the 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);
}
}