Hi,
I just want to know what is the programme code to make delay in the program for at least 1 second. Is there any simple example to show me? Thanks for helping.
How to use a timer?
Started by mechslayer, Dec 25 2008 10:01 AM
10 replies to this topic
#1
Posted 25 December 2008 - 10:01 AM
|
|
|
#2
Posted 26 December 2008 - 08:49 PM
I'm no programming virtuoso so if anyone gives you another method, please use theirs.
Personally, what i would do is set the interval on the timer to 1000ms and place the code you want run inside the Sub created when double clicked, tick. Don't forget to enable the timer!
Edit
I'm sorry, I misunderstood your post!!
Once again, if anyone tells you otherwise, go with their method but...
Like before, set the intervals to 1000 and go into the timer's 'Tick' routine. In there, write the code you would like executed after 1 second and then disable the timer's counter to stop the code from executing.
I hope this helps. Take Care!
Personally, what i would do is set the interval on the timer to 1000ms and place the code you want run inside the Sub created when double clicked, tick. Don't forget to enable the timer!
Edit
I'm sorry, I misunderstood your post!!
Once again, if anyone tells you otherwise, go with their method but...
Like before, set the intervals to 1000 and go into the timer's 'Tick' routine. In there, write the code you would like executed after 1 second and then disable the timer's counter to stop the code from executing.
I hope this helps. Take Care!
#3
Posted 27 December 2008 - 10:58 AM
Hi,
Thanks for answering.. Actually currently I using a used a code example provided by the Microsoft.
By taking things I don't need, its look like this:
Public Class Main
Private Shared myTimer As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
myTimer.Stop()
exitFlag = True
End Sub
Public Shared Sub Timer()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
' Sets the timer interval to 1 seconds.
myTimer.Interval = 2000
myTimer.Start()
' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While
End Sub
Then whenever I need a delay, I will just call Timer() and
myTimer.Enabled = True 'Restart the timer
DataEntered = False
exitFlag = False 'Re-enable the timer
Its works actually but for no reason, its actually failed after few tries (Like maybe 5 times). After its failed, its back as per normal. Sometime 10 times before its failed. What went wrong is that no delay or prolonged delay. If something like that happened right away I at least can try figure out, but its a random scenario which I can't figure out.
Thats the reason I wanted to know whether is there any more simple delay method? I just start learning programming so I don't know how a timer code look like in Visual Basic.
Thanks for answering.. Actually currently I using a used a code example provided by the Microsoft.
By taking things I don't need, its look like this:
Public Class Main
Private Shared myTimer As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
myTimer.Stop()
exitFlag = True
End Sub
Public Shared Sub Timer()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
' Sets the timer interval to 1 seconds.
myTimer.Interval = 2000
myTimer.Start()
' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While
End Sub
Then whenever I need a delay, I will just call Timer() and
myTimer.Enabled = True 'Restart the timer
DataEntered = False
exitFlag = False 'Re-enable the timer
Its works actually but for no reason, its actually failed after few tries (Like maybe 5 times). After its failed, its back as per normal. Sometime 10 times before its failed. What went wrong is that no delay or prolonged delay. If something like that happened right away I at least can try figure out, but its a random scenario which I can't figure out.
Thats the reason I wanted to know whether is there any more simple delay method? I just start learning programming so I don't know how a timer code look like in Visual Basic.
#4
Posted 27 December 2008 - 01:10 PM
It's actually much more simple.
1) Add a Timer control from the toolbox.
2) In the Properties window, set the Interval to however milliseconds you want it to wait.
3) Double click the timer at the bottom of the screen. The view will switch to Code view, and you will notice a new method at the bottom, that looks something like this:
Simply insert any code in there, and it will be executed after you set Timer1.Enabled to True.
1) Add a Timer control from the toolbox.
2) In the Properties window, set the Interval to however milliseconds you want it to wait.
3) Double click the timer at the bottom of the screen. The view will switch to Code view, and you will notice a new method at the bottom, that looks something like this:
Private Sub Timer1_Tick(ByVal e As EventArgs, ByVal sender As Object) Handles Timer1.Tick //Code goes here End Sub
Simply insert any code in there, and it will be executed after you set Timer1.Enabled to True.
#5
Posted 29 December 2008 - 12:24 AM
Is there any way to activate a timer without affecting the programme, Only when the time up then activate the code within?
#7
Posted 01 January 2009 - 11:10 AM
I not sure whether is that what I want but I will try. Thanks for all help, get back to here when I still have trouble.. =D
#9
Posted 08 January 2009 - 04:03 AM
I suppose I should explain what a timer is first right? Well a timer is another one of those useful controls within VB that allows the developer to make things happen after a certain time, or on a certain event. You’ll understand a little better as we go on.
First of all fire up VB, whatever version your using and we’ll get to work. Make one button, there is no need to do anything to fancy since all we are doing is showing the use of timers. Enter the following code under the button_click sub.
To add the timer, just double click on the clock icon from your toolbox (Usually situated on the left hand side of the screen), this just adds the control the current form in use. Ok, so we have the timer all set and ready to go, time to get to work. First of all we should disable the timer, since we don’t want it to just do its thing without us telling it to right? A note here, if you want the timer to do something say after a certain period of time after form load then don’t use this code.
First of all fire up VB, whatever version your using and we’ll get to work. Make one button, there is no need to do anything to fancy since all we are doing is showing the use of timers. Enter the following code under the button_click sub.
MsgBox ("HELLO I'M A MSGBOX PROMPT")
No need to explain what this does to you guys eh? So, when we click the button the program prompts you. So, all we need to do now it add a timer and make it use the timer.To add the timer, just double click on the clock icon from your toolbox (Usually situated on the left hand side of the screen), this just adds the control the current form in use. Ok, so we have the timer all set and ready to go, time to get to work. First of all we should disable the timer, since we don’t want it to just do its thing without us telling it to right? A note here, if you want the timer to do something say after a certain period of time after form load then don’t use this code.
Private Sub Form_Load() Timer1.Enabled = False End SubAll this does is stop the timer from doing anything until we make it do something. First of all delete the button_click code so it looks like this.
Private Sub Command1_Click() Timer1.Enabled = True End SubThis tells the program that when the user clicks the button to do whatever is in the timer sub, which in this case is this.
Private Sub Timer1_Timer()
MsgBox ("HELLO I'M A MSGBOX PROMPT")
Timer1.Enabled = False
End Sub
This first of all prompts the user with the HELLO I’M A MSGBOX PROMPT message, and then disables the timer; this stops it from repeating itself endlessly. Now onto changing the properties. The interval is the property we are concerned with, since this sets the delays. It’s default unit is ms. Have a play around with it, and then run the program.
#10
Posted 08 January 2009 - 05:35 PM
This is really good! Haha.. thanks for putting the effort in making this guide. I will go have some fun with it right away. Will post again if any questions. =D
#11
Posted 08 January 2009 - 09:07 PM
Yeah definitely and thanks for your response....


Sign In
Create Account


Back to top









