vb.net如何仅在特定时间之间执行任务

时间:2016-04-29 06:55:58

标签: vb.net

我有一个私有子Count_Average可以执行操作,我有一个计时器TimerAverage,它将执行Count_Average。现在它每分钟执行一次,每周7天,每天24小时 - 但我希望它只在特定时间之间执行。这将是07:00 - 23:59:59之间。我怎样才能设置“属性”?

以下是我的TimerAverage.tick目前所做的事情以及我正在努力实现的目标。

Private Sub TimerAverage_Tick(sender As Object, e As EventArgs) Handles TimerAverage.Tick

Dim dateValue As DateTime = DateTime.Now
Dim startTime As DateTime = dateValue.Date + New TimeSpan(07, 0, 0)
Dim endTime As DateTime = dateValue.Date + New TimeSpan(23, 59, 0)

''''' Below is a demonstration of what I want (which obviously don't work)

''''' If not datetime.now is between starttime 
'     Do nothing

'else

  TimerAverage.Stop()
  Count_Average()
  TimerAverage.Start()

'end if

我知道这是一次糟糕的尝试,它更像是我想要完成的事情。我试图弄清楚如何正确地写这个,但我无法弄明白。

1 个答案:

答案 0 :(得分:4)

如果范围为7:00 - 23:59,您只需使用:

If DateTime.Now.Hour >= 7 Then
    'do something
End If