如何在 VB.NET 中每 x 分钟调用一次函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8813004/
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
How do I call a function every x minutes in VB.NET?
提问by bear
How do I call a function every x minutes.
我如何每 x 分钟调用一个函数。
I assume I'd have to add a timer to the form?
我假设我必须在表单中添加一个计时器?
回答by John Woo
here is a simple example:
这是一个简单的例子:
Public Class SampleCallEveryXMinute
Private WithEvents xTimer as new System.Windows.Forms.Timer
Public Sub New(TickValue as integer)
xTimer = new System.Windows.Forms.Timer
xTimer.Interval = TickValue
End Sub
Public Sub StartTimer
xTimer.Start
End Sub
Public Sub StopTimer
xTimer.Stop
End Sub
Private Sub Timer_Tick Handles xTimer.Tick
SampleProcedure
End Sub
Private Sub SampleProcedure
'SomeCodesHERE
End Sub
End Class
USAGE:
用法:
Dim xSub as new SampleCallEveryXMinute(60000) ' 1000 ms = 1 sec so 60000 ms = 1 min
xSub.StartTimer
回答by William Lawn Stewart
Yes, you could add a timer to the form, and set its interval to x*60000, where x is the number of minutes between calls.
是的,您可以在表单中添加一个计时器,并将其间隔设置为 x*60000,其中 x 是通话之间的分钟数。
Remember that the timer runs on the UI thread, so don't do anything intensive in the function. Also, if the UI thread is busy, the timer event will not fire until the UI thread finishes whatever event it is currently processing. If your function is going to be CPU-intensive, then consider having the timer start up a background worker
请记住,计时器在 UI 线程上运行,因此不要在函数中执行任何密集操作。此外,如果 UI 线程繁忙,则在 UI 线程完成当前正在处理的任何事件之前,不会触发计时器事件。如果您的功能将是 CPU 密集型的,那么请考虑让计时器启动后台工作程序
If you require a longer time period between function calls (ie, one thats too big for a timer interval) then you could have a timer function that fires every minute, and increments a counter until the desired amount of time has passed, before going on to call the function.
如果您在函数调用之间需要更长的时间段(即,对于计时器间隔来说太大了),那么您可以拥有一个每分钟触发一次的计时器函数,并在继续之前增加一个计数器直到经过所需的时间来调用函数。
回答by parapura rajkumar
ALTERNATIVE 1
备选方案 1
Here is good guide to use the Timer Controlin VB.net.
这是在 VB.net 中使用计时器控件的好指南。
The advantage is that you don't have to worry about modifying UI objects from non UI thread.
优点是您不必担心从非 UI 线程修改 UI 对象。
ALTERNATIVE 2
备选方案 2
Another alternative is to spawn another thread and do the work and sleep the remaining x minutes.
另一种选择是产生另一个线程并完成工作并在剩余的 x 分钟内休眠。
The advantage here is that if your function doesn't touch UI objects your application will remain responsive to user input while the function is being called
这里的优点是,如果您的函数不接触 UI 对象,您的应用程序将在调用函数时保持对用户输入的响应
回答by Afshin
Private Sub Form_Load()
_timer.Enabled = True
End Sub
Private Sub _timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(DateTime.Now.ToLongTimeString() + "," + DateTime.Now.ToLongDateString())
End Sub