System.Timer.Timer先执行后等待

hyunwung 2008-07-15 11:54:43
System.Timer.Timer控件的问题。

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秒后)->然后每隔两秒执行一次。
...全文
1066 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
GhostAdai 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hyunwung 的回复:]
引用 5 楼 gomoku 的回复:
...OnTimedEvent是我写在外面的函数,...
函数, so why don't you call it once at the begining?

如上所说不是不行,不过投机取巧了,不是我想要的结果。
[/Quote]

晕,很多算法都是投机取巧的,能出来就行了,何必执着呢?
hyunwung 2008-07-15
  • 打赏
  • 举报
回复
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!");
}
}
hyunwung 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 Macosx 的回复:]
楼主要求的不是正常的用法 所以任何解决方法都是取巧的 那还问大家干啥
[/Quote]
也就是说,timer只能是先等待,然后再执行,我说的用timer属性控制是无法实现的??
Macosx 2008-07-15
  • 打赏
  • 举报
回复
楼主要求的不是正常的用法 所以任何解决方法都是取巧的 那还问大家干啥
gomoku 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hyunwung 的回复:]
So what is the difference?
You've got what you want.

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
[/Quote]

Sure, that is your program :-)
hyunwung 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gomoku 的回复:]
引用 6 楼 hyunwung 的回复:
如上所说不是不行,不过投机取巧了,不是我想要的结果。


So what is the difference?
You've got what you want.
[/Quote]
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
gomoku 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hyunwung 的回复:]
如上所说不是不行,不过投机取巧了,不是我想要的结果。
[/Quote]

So what is the difference?
You've got what you want.
hyunwung 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gomoku 的回复:]
...OnTimedEvent是我写在外面的函数,...
函数, so why don't you call it once at the begining?
[/Quote]
如上所说不是不行,不过投机取巧了,不是我想要的结果。
gomoku 2008-07-15
  • 打赏
  • 举报
回复
...OnTimedEvent是我写在外面的函数,...
函数, so why don't you call it once at the begining?
hyunwung 2008-07-15
  • 打赏
  • 举报
回复
不太明白gomoku的意思。OnTimedEvent是我写在外面的函数,
OnTimedEvent(this, ElapsedEventArgs.Empty);是什么意思
hyunwung 2008-07-15
  • 打赏
  • 举报
回复

// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
gomoku 2008-07-15
  • 打赏
  • 举报
回复

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);
freeboy827 2008-07-15
  • 打赏
  • 举报
回复
System.Timers.Timer aTimer = new System.Timers.Timer();
初始化时写好参数就行了,不要用无参数的构造函数
jin1002 2008-07-15
  • 打赏
  • 举报
回复
那改用线程吧
就可以先执行再等待了
gomoku 2008-07-15
  • 打赏
  • 举报
回复
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

hyunwung 2008-07-15
  • 打赏
  • 举报
回复
有说system.thread.timer能够控制是先等待还是先执行……?
loveAllenSu 2008-07-15
  • 打赏
  • 举报
回复
看的都头大!哈哈

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧