循环检查VB.NET中的时间 [英] Loop to check time in VB.NET

查看:151
本文介绍了循环检查VB.NET中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是一个新的VB,我只是在一个小项目玩,我目前需要一个循环,不断检查系统时钟,看看是否等于一定的时间。

 而不是myTime.Hour = 24 

如果TimeOfDay = newTime然后
nfi.ShowBalloonTip(15)
intRandNumb = RandomNumber(1,15)
dblAddMinutes = intTime + intRandNumb
newTime = TimeOfDay.AddMinutes(dblAddMinutes)

End If
End While

我现在有这个权限,但显然它正在研磨所有的东西停止,并在进程中使用我的cpu的50%我只想知道我可以替代或改变,使这个循环运行得更好,并执行我需要的方式。

解决方案

正如乔尔指出的那样,你应该尝试使用一个计时器。我不知道你的应用程序是一个表单或控制台或其他,所以我会尝试通用,并使用 System.Timers.Timer



这里的代码(间隔设置为10ms,更改为需要的值):

  Private timer1 As System.Timers.Timer 
Const interval As Integer = 10


Sub initTimer()
timer1 = New System.Timers.Timer 10)
AddHandler timer1.Elapsed,AddressOf Me.timer_Elapsed
timer1.Start()
End Sub

Sub timer_Elapsed(ByVal sender As Object,ByVal e As System $ T

'Console.WriteLine(e.SignalTime.ToString())
End Sub


So I'm kind of new to VB and am just playing around with a little project, I currently need a loop that is constantly checking the systems clock to see if it's equal to a certain time.

   While Not myTime.Hour = 24

        If TimeOfDay = newTime Then
            nfi.ShowBalloonTip(15)
            intRandNumb = RandomNumber(1, 15)
            dblAddMinutes = intTime + intRandNumb
            newTime = TimeOfDay.AddMinutes(dblAddMinutes)

        End If
    End While

I have this right now, but obviously it's grinding everything to a halt and using 50% of my cpu in the process, I just would like to know what I can substitute in or change to make this loop run better and perform how I need it to.

解决方案

As Joel pointed out, you should try using a timer instead. I'm not sure if your app is a form or console or other, so I'll try to be generic and use System.Timers.Timer.

The code here (interval is set at 10ms, change to a value of your need):

Private timer1 As System.Timers.Timer    
Const interval As Integer = 10 


Sub initTimer()
    timer1 = New System.Timers.Timer(10)
    AddHandler timer1.Elapsed, AddressOf Me.timer_Elapsed
    timer1.Start()
End Sub

Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    'do your stuff here
    'Console.WriteLine(e.SignalTime.ToString())
End Sub

这篇关于循环检查VB.NET中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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