本文介绍了循环计时器在一个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 如何将计时器循环到一定时间? 让''我说我每5秒运行一次计时器,持续30秒。当时间等于30秒时,它将执行其余的代码。我只有这个想法: - Dim WithEvents myTimer As 新计时器 Dim iTimer As 长 myTimer.Enabled = True myTimer.Interval = 5000 如何从此变量启动代码?循环myTimer并输入iTimer,当iTimer等于30000然后我的代码从那里开始.. 解决方案 您需要知道计时器在应用程序中生成重复事件。在其他恶化中:计时器提供了以指定间隔执行方法的机制。看看这里:计时器类(System.Timers) [ ^ ]和定时器类(System.Threading) [ ^ ] 引用:当iTimer等于30000然后我的代码从那里开始 你在谈论睡眠 [ ^ ] function? 我得到了这个小班,应该是完美的满足你的需求。 (我只改变语言,所以它应该运行正常) 进口系统。 Windows.Forms 命名空间 Klassen 公共 类 Timer_Var ''' < 摘要 > ''' 计时器对象 ''' < / summary > 私人 WithEvents _tmr 正如 新计时器使用 {.Interval = 100 } ''' < 摘要 > ''' 定时器的间隔(默认为100毫秒) ''' < / summary > 公开 属性区间 As 整数 获取 返回 _tmr.Interval 结束 获取 设置( ByVal 值正如 整数) _tmr.Interval = value 结束 设置 结束 物业 ''' < 摘要 > ''' 是否正在运行计时器? ''' < / summary > 公开 ReadOnly 属性已启用 As 布尔 获取 返回 _tmr.Enabled 结束 获取 结束 属性 ''' < 摘要 > ''' 定时器'循环' event ''' < / summary > 公开 事件勾选作为 EventHandler ''' < summary > ''' 由于结束时间,计时器已结束 ''' < / summary > 公开 事件结束作为 EventHandler ''' < 摘要 > ''' 手动停止计时器 ''' < / summary > 公开 事件已取消 A s EventHandler 私有 _timer_end As 日期 私有 _timer_start 作为 日期 ''' < 摘要 > ''' 秒计时器已经全部运行 ''' < / summary > 公开 ReadOnly 属性 running_since 作为 整数 获取 Dim 此 As 新 TimeSpan this = Now - _timer_start 返回 CInt (this.TotalSeconds) 结束 获取 结束 物业 ''' < 摘要 > ''' 秒计时器仍在运行(如果无限则为-1) ''' < / summary > 公开 ReadOnly Property running_still As Integer 获取 Dim 此作为 新 TimeSpan 如果 _timer_end< _timer_start 然后 返回 -1 ' 无限 结束 如果 this = _timer_end - 现在 返回 CInt (this.TotalSeconds) 结束 获取 结束 属性 ''' < 摘要 > ''' 总计计时器的运行时间(-1,如果无限) ''' < / summary > 公共 ReadOnly 属性 running_total 作为 整数 获取 Dim 此作为 新 TimeSpan 如果 _timer_end< _timer_start 然后 返回 -1 ' 无限 结束 如果 this = _timer_end - _timer_start 返回 CInt (this.TotalSeconds) 结束 获取 结束 属性 ''' < 摘要 > ''' 启动给定秒的计时器 ''' < / summary > ''' < param name =Dauer > 以秒为单位的时间< / param > 公开 Sub 开始( ByVal Dauer 作为 Double ) _timer_start =现在 _timer_end = _timer_start.AddSeconds(Dauer) _tmr.Start() 结束 Sub ''' < ; 摘要 > ''' 启动计时器无限 ''' < / summary > 公开 Sub Start() _timer_start =现在 _timer_end = _timer_start.AddHours(-1)' 过去的结束时间,意味着无限的运行时间 _tmr.Start() 结束 Sub ''' < 摘要 > ''' 停止计时器 ''' < / summary > 公开 Sub [停止]() _tmr。停止() RaiseEvent 已取消(我,新 EventArgs) 结束 Sub 私有 Sub tmr_Tick( ByVal 发​​件人作为 对象, ByVal e As EventArgs)句柄 _tmr.Tick RaiseEvent 勾选(我,e) 如果 _timer_end< ; =现在 AndAlso _timer_end> _timer_start 然后 ' 我们已经到了最后。 _tmr。停止() RaiseEvent 结束( Me ,e) Else ' 运行无限... 结束 如果 结束 Sub 结束 类 结束 命名空间 Hi all,How do I loop timer until certain time?Let''s say I run a timer every 5 seconds for 30 seconds. When the time equal to 30 seconds, it will do the rest of the code..I have this idea only:-Dim WithEvents myTimer As New TimerDim iTimer As LongmyTimer.Enabled = TruemyTimer.Interval = 5000How do I start my code from this variable? To loop the myTimer and put into iTimer and when iTimer equal to 30000 Then my code start there.. 解决方案 You need to know that timer generates recurring events in an application. In other wors: timer provides a mechanism for executing a method at specified intervals. Have a look here: Timer Class (System.Timers)[^] and here Timer Class (System.Threading)[^]Quote:when iTimer equal to 30000 Then my code start thereAre you talking about Sleep[^] function?I got this little class, should be perfect for your needs.(I only changed the language, so it should run fine)Imports System.Windows.FormsNamespace Klassen Public Class Timer_Var ''' <summary> ''' Timer Object ''' </summary> Private WithEvents _tmr As New Timer With {.Interval = 100} ''' <summary> ''' The Interval of the Timer (100ms default) ''' </summary> Public Property Interval As Integer Get Return _tmr.Interval End Get Set(ByVal value As Integer) _tmr.Interval = value End Set End Property ''' <summary> ''' is the timer running? ''' </summary> Public ReadOnly Property Enabled As Boolean Get Return _tmr.Enabled End Get End Property ''' <summary> ''' Timer 'loop' event ''' </summary> Public Event Tick As EventHandler ''' <summary> ''' Timer has ended due to endtime ''' </summary> Public Event Ended As EventHandler ''' <summary> ''' Timer was stoped manually ''' </summary> Public Event Canceled As EventHandler Private _timer_end As Date Private _timer_start As Date ''' <summary> ''' seconds the timer is allready running ''' </summary> Public ReadOnly Property running_since As Integer Get Dim this As New TimeSpan this = Now - _timer_start Return CInt(this.TotalSeconds) End Get End Property ''' <summary> ''' Seconds the timer is still running (-1 if infinite) ''' </summary> Public ReadOnly Property running_still As Integer Get Dim this As New TimeSpan If _timer_end < _timer_start Then Return -1 'infinite End If this = _timer_end - Now Return CInt(this.TotalSeconds) End Get End Property ''' <summary> ''' Total runtime of the timer (-1 if infinite) ''' </summary> Public ReadOnly Property running_total As Integer Get Dim this As New TimeSpan If _timer_end < _timer_start Then Return -1 'infinite End If this = _timer_end - _timer_start Return CInt(this.TotalSeconds) End Get End Property ''' <summary> ''' Starts the timer for given Seconds ''' </summary> ''' <param name="Dauer">Time in Seconds</param> Public Sub Start(ByVal Dauer As Double) _timer_start = Now _timer_end = _timer_start.AddSeconds(Dauer) _tmr.Start() End Sub ''' <summary> ''' Starts the timer infinite ''' </summary> Public Sub Start() _timer_start = Now _timer_end = _timer_start.AddHours(-1) 'endtime in the past, means infinite runtime _tmr.Start() End Sub ''' <summary> ''' Stops the Timer ''' </summary> Public Sub [Stop]() _tmr.Stop() RaiseEvent Canceled(Me, New EventArgs) End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles _tmr.Tick RaiseEvent Tick(Me, e) If _timer_end <= Now AndAlso _timer_end > _timer_start Then 'we have reached the end. _tmr.Stop() RaiseEvent Ended(Me, e) Else 'runing infinite... End If End Sub End ClassEnd Namespace 这篇关于循环计时器在一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 23:56
查看更多