Answered by:
Inserting a delay

Question
-
I want to create a delay of X seconds in some code I have written however I am not sure how to do it. Any help would be appreciated.
eg.
CheckTemperature()
delay
CheckTemperature()
delay
CheckTemperature()
delay
CheckTemperature()
Wednesday, May 13, 2009 7:41 AM
Answers
-
Using the timer would be preferable to using Thread.Sleep as it will leave your GUI responsive which Thread.Sleep will not.
Dim WithEvents Tmr As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tmr.Interval = 1000
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
CheckTemperature()
End Sub- Proposed as answer by A.m.a.L Hashim Wednesday, May 13, 2009 11:32 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 9:50 AM -
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thanks,
A.m.a.L
.Net Goodies
[Remember to click "mark as answered" when you get a correct reply to your question]- Proposed as answer by A.m.a.L Hashim Wednesday, May 13, 2009 11:32 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 7:52 AM -
Dim WithEvents myTimer As New Timer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click myTimer.Interval = 2000 '2 seconds sub1() End Sub Private Sub sub1() 'work 'work 'work myTimer.Enabled = True End Sub Private Sub sub2() MsgBox("hi") End Sub Private Sub myTick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick myTimer.Enabled = False sub2() End Sub
Thanks
- Omie- Proposed as answer by Omie Monday, May 18, 2009 6:53 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 2:49 PM
All replies
-
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thread.Sleep(1000) '1000 milliseconds
CheckTemperature()
Thanks,
A.m.a.L
.Net Goodies
[Remember to click "mark as answered" when you get a correct reply to your question]- Proposed as answer by A.m.a.L Hashim Wednesday, May 13, 2009 11:32 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 7:52 AM -
I want to create a delay of X seconds in some code I have written however I am not sure how to do it. Any help would be appreciated.
eg.
CheckTemperature()
delay
CheckTemperature()
delay
CheckTemperature()
delay
CheckTemperature()
You can also add a timer and use its tick even to execute a set of statements in specified time interval.
Thanks,
A.m.a.L
.Net Goodies
[Remember to click "mark as answered" when you get a correct reply to your question]- Proposed as answer by A.m.a.L Hashim Wednesday, May 13, 2009 11:32 AM
Wednesday, May 13, 2009 7:53 AM -
Using the timer would be preferable to using Thread.Sleep as it will leave your GUI responsive which Thread.Sleep will not.
Dim WithEvents Tmr As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tmr.Interval = 1000
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
CheckTemperature()
End Sub- Proposed as answer by A.m.a.L Hashim Wednesday, May 13, 2009 11:32 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 9:50 AM -
I asked this question earlier but I did not ask the right thing.
I need to put a delay into my application where after Function A runs I need to run Function B a variable number of seconds later.
e.g. Function A - turn on switch ..... wait some time ..... Function B - check if the result has been achieved
The application will not require any user input during this delay if that makes a difference. I tried using the timer option but had trouble getting a good grasp of it and I only want it to perform Function B once and I was having trouble with that as it would repeat every interval.- Merged by Riquel_DongModerator Friday, May 15, 2009 4:13 AM don't post the duplicate thread
Wednesday, May 13, 2009 1:39 PM -
Dim WithEvents myTimer As New Timer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click myTimer.Interval = 2000 '2 seconds sub1() End Sub Private Sub sub1() 'work 'work 'work myTimer.Enabled = True End Sub Private Sub sub2() MsgBox("hi") End Sub Private Sub myTick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick myTimer.Enabled = False sub2() End Sub
Thanks
- Omie- Proposed as answer by Omie Monday, May 18, 2009 6:53 AM
- Marked as answer by Riquel_DongModerator Tuesday, May 19, 2009 10:05 AM
Wednesday, May 13, 2009 2:49 PM -
Assuming Function A runs when a button is clicked (or something similar):
At the end of Function A, enable the timer (Timer1.Enabled = True). Set your timer's interval to however many milliseconds you want (5000 milliseconds = 5 secs). In the Timer1.Tick put your code for Function B or a call to Function B, then at the end of this disable the timer (Timer1.Enabled = False).Wednesday, May 13, 2009 2:52 PM -
then at the end of this disable the timer (Timer1.Enabled = False).
If execution of B() lasts for more than interval then next tick is processed, need to stop it before calling B() :-)
Thanks
- OmieWednesday, May 13, 2009 2:57 PM -
Omie, good point
Didn't consider that - can't think of a time where I've had a process take longer than maybe a second. But it is a possibility.
Thanks for the catchWednesday, May 13, 2009 3:50 PM -
Put a msgbox inside B() and when it pops up, keep it as it is and let it pass interval time :-p
Thanks
- OmieWednesday, May 13, 2009 4:27 PM -
Dave 299,
I'd like to thank you. I'm fairly new to programming in Visual Basic. I'm currently using VB 2008. I've been racking my brain trying to figure out how to use the Timer class. The example, and all examples, it seems, that MSDN provides is insanely complicated. I don't know why they have to make all their examples so complicated. Your example above made it simple enough for me to get it working.
I'm trying to rotate through characters in a textbox, and incorporating the timer in a class. I think with your example above, finally I hear one shoe falling.
Thursday, January 10, 2013 12:17 AM -
All righty then, the second shoe has fallen.
There are two types of timer classes. I wanted to use class Timer in a class. I tried to use the Tick event, and it didn't recognize it. Then I noticed that for my object, Intellisense wasn't showing Tick as an option. Then I went back and read some more from MSDN. From what I read, the class System.Timers.Timer doesn't have a Tick event, only an Elapsed event. Then why did Tick work in the form code.
Form code uses Windows.Form.Timer.
Great. I obviously have much to learn.
Friday, January 11, 2013 12:33 AM