计时器事件 [英] Timer Events

查看:136
本文介绍了计时器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对应该容易的事情有疑问.我正在使用计时器(timer1)和秒表来控制定时事件.

I am having a problem with what should be easy. I am using a timer (timer1) and stopwatches to control timed events.

<br />
    Dim time As New Stopwatch<br />
    Dim holdtime As New Stopwatch<br />
    Dim savetime As New Stopwatch<br />


<br />
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br />
        ''hide label1 after 3 seconds.<br />
        If time.Elapsed.Seconds = 3 Then<br />
            If Label1.Visible = True Then<br />
                Label1.Visible = False<br />
            End If<br />
        End If<br />
        time.Reset() : time.Start()<br />
<br />
        ''update ''hold'' every minute<br />
        If holdtime.Elapsed.Minutes = 1 Then<br />
            hold = MainText.Text<br />
        End If<br />
        holdtime.Reset() : holdtime.Start()<br />
<br />
        ''autosave every 5 minutes.<br />
        If savetime.Elapsed.Minutes = 5 Then<br />
            If current = "" Then ''no autosave if file hasn''t been saved already.<br />
                Exit Sub<br />
            Else ''save the file as previously named.<br />
                Dim myStream As StreamWriter = New StreamWriter(current, False)<br />
                myStream.Write(MainText.Text)<br />
                myStream.Close()<br />
            End If<br />
            savetime.Reset() : savetime.Start()<br />
        End If<br />
    End Sub<br />


我在这里做错了什么?


What am I doing wrong here?

推荐答案

确切的问题尚不清楚,但是您正在使用计时器控件,就好像它确实很复杂.好吧,计时器无法保证如此,您也不能指望在您指定的时间被准确执行.因此,如有必要,请使用范围,或者简单地使用大于,这对您而言就足够了.

将=更改为> =
The exact problem isn''t clear, but you are using the timer control as if it was really something sophisticated. Well, the timer comes with no guarantee what so ever and you can''t count on being executed exactly at the time you specified. So use ranges if necessary or simply use greater-than, which would be sufficient in your case.

Change = into >=
If time.Elapsed.Seconds >= 3 Then



同样,为不同事件设置一些计时器并设置大约所需的正确时间可能会更简单.这样更易于维护,更易于操作,并且不会给您带来性能/资源上的损失.

祝你好运!



Also, it might be simpler to have some timers for different events set with the correct timing you approximately want. This much easier to maintain, easier on the eyes and it won''t give you a performance/resource penalty.

Good luck!


这篇关于计时器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆