Timers.Timer

pumaadamsjack 2012-03-23 08:51:50
用.net 做一个windows服务,目的是定时执行一些事情,比如清理一些日志,处理一些数据问题等。

看了些文章,基本好像是使用
System.Timers.Timer 来实现。

代码大概是


System.Timers.Timer t = new System.Timers.Timer(sleeptime);//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(rebootsystem);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;

但我的问题是

我定义了 System.Timers.Timer t = new System.Timers.Timer(sleeptime)

但在t这个实例中,竟然没有 Elapsed 。我尝试了控制台程序,也没有。


非常奇怪啊,我查msdn 明明是有这个事件的啊。

请高手指点


我使用的环境是visual studio 2010 分别用.net 2.0 和 4.0 都试过
...全文
143 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
烈火蜓蜻 2012-03-23
  • 打赏
  • 举报
回复
有个组件非常适合你的要求。

http://blog.csdn.net/linux7985/article/details/7381817
pumaadamsjack 2012-03-23
  • 打赏
  • 举报
回复
是我对vb.net的委托写法不了解,惭愧,好在遇到高人了。。


[Quote=引用 9 楼 bdmh 的回复:]

引用 6 楼 pumaadamsjack 的回复:
回一楼,那是我粘贴的别人的代码


我今天早上最新试了一下,发现了问题。我使用vb.net创建的项目里就是没有这个方法。

我重新使用c#创建的项目,就有!!!!

请问各位难道这个和语言有关系吗?

vb代码不是这么写啊,你看帮助,我给你贴
VB.NET code

Imports System
Imports Sys……
[/Quote]
顽主小贝 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bdmh 的回复:]
引用 6 楼 pumaadamsjack 的回复:
回一楼,那是我粘贴的别人的代码


我今天早上最新试了一下,发现了问题。我使用vb.net创建的项目里就是没有这个方法。

我重新使用c#创建的项目,就有!!!!

请问各位难道这个和语言有关系吗?

vb代码不是这么写啊,你看帮助,我给你贴

VB.NET code

Imports System
Imports ……
[/Quote]
大神,刚才多有得罪啊,小弟现在也有一个问题想请教请教你(公司不让上网,很期待能认识你O,小弟·QQ121056759)、、
bdmh 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pumaadamsjack 的回复:]
回一楼,那是我粘贴的别人的代码


我今天早上最新试了一下,发现了问题。我使用vb.net创建的项目里就是没有这个方法。

我重新使用c#创建的项目,就有!!!!

请问各位难道这个和语言有关系吗?
[/Quote]
vb代码不是这么写啊,你看帮助,我给你贴

Imports System
Imports System.Timers

Public 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
'...
arjo_zhong 2012-03-23
  • 打赏
  • 举报
回复
.Net 4.0

System.Timers.Timer timer = new System.Timer.Time(times)


这句完全没问题

用线程 等待 会好点麽?

thread.sleep(times);
EnForGrass 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 pumaadamsjack 的回复:]
用.net 做一个windows服务,目的是定时执行一些事情,比如清理一些日志,处理一些数据问题等。

看了些文章,基本好像是使用
System.Timers.Timer 来实现。

代码大概是


System.Timers.Timer t = new System.Timers.Timer(sleeptime);//实例化Timer类,设置间隔时间为10000毫秒;
……
[/Quote]
我试了一下3.5没问题,4.0的话应该也没问题啊!
pumaadamsjack 2012-03-23
  • 打赏
  • 举报
回复
回一楼,那是我粘贴的别人的代码


我今天早上最新试了一下,发现了问题。我使用vb.net创建的项目里就是没有这个方法。

我重新使用c#创建的项目,就有!!!!

请问各位难道这个和语言有关系吗?
bdmh 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xb550453621 的回复:]
是不是要加引用?鄙视一楼,既然有,你为什么不告诉人家怎么调用、、、
[/Quote]
你眼睛有问题啊,楼主代码已经列出了,看不见啊
顽主小贝 2012-03-23
  • 打赏
  • 举报
回复
using System.Timers;
加上这句试试
只在此山中 2012-03-23
  • 打赏
  • 举报
回复
大小写错误?
顽主小贝 2012-03-23
  • 打赏
  • 举报
回复
是不是要加引用?鄙视一楼,既然有,你为什么不告诉人家怎么调用、、、
bdmh 2012-03-23
  • 打赏
  • 举报
回复
有哇,怎么没有

111,126

社区成员

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

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

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