VB.net 在预定时间执行任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17973665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net Perform task at scheduled Time
提问by user2620851
i need to perform a task at a certain time using the using the user inputted Hour and Minute.
我需要使用用户输入的小时和分钟在特定时间执行任务。
I'm thinking of something like using some sort of loop that every time it loops, checks if the Now().Hourmatches HourChoiceand the Now().Minutematches the MinuteChoice
我正在考虑使用某种循环,每次循环时,检查Now().Hour匹配项HourChoice和Now().Minute匹配项是否 匹配MinuteChoice
In this, i also have a user option to chose "AM" or "PM", and if TimeOfDay = "PM", add 12 hours (The Now().Houris military time).
在此,我还有一个用户选项可以选择“AM”或“PM”,如果是TimeOfDay = "PM",则添加 12 小时(这Now().Hour是军用时间)。
EDIT:I only want this to run once at a time, not every day.
编辑:我只希望它一次运行一次,而不是每天运行一次。
回答by randcd
You could use a System.Threading.Timerto do this. Instead of a loop that constantly runs and sleeps, you could set the interval to the Timerto be the difference between now and the selected time and the TimerCallbackto be the Subthat does the task work. Setting the timeout to 0 or Timeout.Infinitewill make sure this is only executed once.
你可以使用 aSystem.Threading.Timer来做到这一点。而不是持续运行和休眠循环的,你可以在间隔设置为Timer为现在选定的时间和之间的区别TimerCallback是Sub,做任务的工作。将超时设置为 0 或Timeout.Infinite将确保只执行一次。
EDIT: Example
编辑:示例
Dim tcb As TimerCallback = AddressOf DoStuff
Dim t As Timer
Dim execTime As TimeSpan
Dim dtNow As DateTime = DateTime.Now
Dim hc As Integer = HourChoice
Dim mc As Integer = MinuteChoice
If TimeOfDay = "AM" And hc = 12 Then
hc = 0
Else If TimeOfDay = "PM" Then
hc = hc + 12
End If
Dim dtCandidate As DateTime = New DateTime(dtNow.Year, dtNow.Month, dtNow.Day, hc, mc, 0)
If dtCandidate < dtNow Then
dtCandidate.AddDays(1)
End If
execTime = dtCandidate.Subtract(dtNow)
t = New Timer(tcb,Nothing,execTime,TimeSpan.Zero)
Then you just need a Sub to do your task:
然后你只需要一个 Sub 来完成你的任务:
Public Sub DoStuff(obj As Object)
'...Do Some Kind of Work
End Sub
回答by Ruben_PH
You dont have to loop,
Simply add a Timer to your form, set its interval to 500 so that it fires every 500 milliseconds.
Enable it via a Button to start your Task.
您不必循环,
只需在表单中添加一个 Timer,将其间隔设置为 500,以便每 500 毫秒触发一次。
通过按钮启用它以启动您的任务。
Then on your Timer.Tick Event:
然后在您的 Timer.Tick 事件上:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Hour(Now) = HourChoice And Minute(Now) = MinuteChoice Then
'Your Task Here
End If
End Sub
回答by David Sdot
Not tested but that should work. In the TimeSpan you can set days, hours, minutes, seconds.
未经测试,但这应该有效。在 TimeSpan 中,您可以设置天、小时、分钟、秒。
Private timer As System.Threading.Timer = New System.Threading.Timer(AddressOf DoWhatever, Nothing, New TimeSpan(0, 10, 0, 0), -1)
Private Sub dowhatever(sender As Object, e As Timers.ElapsedEventArgs)
' Do stuff
End Sub
More can be found on MSDN
可以在MSDN上找到更多信息
回答by Hank
A bit late to add an answer to this question, but: You can use a standard System.Windows.Forms.Timer. Set the interval to the difference in milliseconds beween the required time and Now(), then start the timer.
添加这个问题的答案有点晚了,但是:您可以使用标准的 System.Windows.Forms.Timer。将间隔设置为所需时间与 Now() 之间的毫秒差,然后启动计时器。

