System.Timers.Timer aTimer = new System.Timers.Timer();
// 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();
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
尝试了一下,timer是在线等待2秒之后,去执行的回调函数。启动->(2秒后)->执行OnTimedEvent函数->然后每隔两秒执行一次。
想问的是,可不可以让timer启动就去执行回调函数,然后再开始重复。就是:启动->执行OnTimedEvent函数->(2秒后)->然后每隔两秒执行一次。
17 个解决方案
#1
System.Timers.Timer aTimer = new System.Timers.Timer();
初始化时写好参数就行了,不要用无参数的构造函数
初始化时写好参数就行了,不要用无参数的构造函数
#2
System.Timers.Timer aTimer = new System.Timers.Timer();
// 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;
OnTimedEvent(this, ElapsedEventArgs.Empty); // <----
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
#3
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
#4
不太明白gomoku的意思。OnTimedEvent是我写在外面的函数,
OnTimedEvent(this, ElapsedEventArgs.Empty);是什么意思
OnTimedEvent(this, ElapsedEventArgs.Empty);是什么意思
#5
...OnTimedEvent是我写在外面的函数,...
函数, so why don't you call it once at the begining?
函数, so why don't you call it once at the begining?
#6
如上所说不是不行,不过投机取巧了,不是我想要的结果。
#7
So what is the difference?
You've got what you want.
#8
by my words, how do you know this answer is what i want.
actrually, it's not.i want to know how to set "timer" options to what i want
#9
Sure, that is your program :-)
#10
楼主要求的不是正常的用法 所以任何解决方法都是取巧的 那还问大家干啥
#11
也就是说,timer只能是先等待,然后再执行,我说的用timer属性控制是无法实现的??
#12
using System;
using System.Timers;
public class Timer1
{
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer(20000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
OnTimedEvent(this, ElapsedEventArgs.Empty); //C#在这一句报错了,this指代不明,还有,ElapsedEventArgs向EvenArgs型转化失败
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
#13
晕,很多算法都是投机取巧的,能出来就行了,何必执着呢?
#14
看的都头大!哈哈
#15
有说system.thread.timer能够控制是先等待还是先执行……?
#16
OnTimedEvent(this, ElapsedEventArgs.Empty); //C#在这一句报错了,this指代不明,还有,ElapsedEventArgs向EvenArgs型转化失败
OnTimedEvent(null, null); // it doesn't matter if you don't use those parameters
OnTimedEvent(null, ElapsedEventArgs.Empty); // dittto
#17
那改用线程吧
就可以先执行再等待了
就可以先执行再等待了
#1
System.Timers.Timer aTimer = new System.Timers.Timer();
初始化时写好参数就行了,不要用无参数的构造函数
初始化时写好参数就行了,不要用无参数的构造函数
#2
System.Timers.Timer aTimer = new System.Timers.Timer();
// 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;
OnTimedEvent(this, ElapsedEventArgs.Empty); // <----
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
#3
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
#4
不太明白gomoku的意思。OnTimedEvent是我写在外面的函数,
OnTimedEvent(this, ElapsedEventArgs.Empty);是什么意思
OnTimedEvent(this, ElapsedEventArgs.Empty);是什么意思
#5
...OnTimedEvent是我写在外面的函数,...
函数, so why don't you call it once at the begining?
函数, so why don't you call it once at the begining?
#6
如上所说不是不行,不过投机取巧了,不是我想要的结果。
#7
So what is the difference?
You've got what you want.
#8
by my words, how do you know this answer is what i want.
actrually, it's not.i want to know how to set "timer" options to what i want
#9
Sure, that is your program :-)
#10
楼主要求的不是正常的用法 所以任何解决方法都是取巧的 那还问大家干啥
#11
也就是说,timer只能是先等待,然后再执行,我说的用timer属性控制是无法实现的??
#12
using System;
using System.Timers;
public class Timer1
{
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer(20000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
OnTimedEvent(this, ElapsedEventArgs.Empty); //C#在这一句报错了,this指代不明,还有,ElapsedEventArgs向EvenArgs型转化失败
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
#13
晕,很多算法都是投机取巧的,能出来就行了,何必执着呢?
#14
看的都头大!哈哈
#15
有说system.thread.timer能够控制是先等待还是先执行……?
#16
OnTimedEvent(this, ElapsedEventArgs.Empty); //C#在这一句报错了,this指代不明,还有,ElapsedEventArgs向EvenArgs型转化失败
OnTimedEvent(null, null); // it doesn't matter if you don't use those parameters
OnTimedEvent(null, ElapsedEventArgs.Empty); // dittto
#17
那改用线程吧
就可以先执行再等待了
就可以先执行再等待了