Help with System.Timers.Timer in a module

S

Steve Lowe

Hi,

I'm getting into VB.net with the help of a few books, but have got a
problem grasping how to implement a system.timers.timer

Only 1 of my 3 books mentions timers and that's a Windows.Form.Timer

Basically I need to have a 30 second delay in some module code.

To help me get the hang of it - (that should really be start to get
the hang of it !!) could someone help me with some code that does the
following


Thanks

splowe.



Module Module1

Sub Main ()

MessageBox.Show("OK To start the Delay")

pause for 30 secs

MessageBox.Show("30 second delay has finished")

End Sub
End Module




- Steve Lowe
- E-Mail : (e-mail address removed)
- Before Replying Remove .NO.SPAM
- UK Resident although my e-mail address is usa.net
 
M

Miro

Make sure you set your timer interval to be a 1000 ( 1 second )
Make a variable to hold your "30" value.

And before you message box out, if you do not enable=false the timer, the
timer keeps counting.
Thats why its disabled and then enabled again after the fact.

Miro

Public TimerVal As Integer = 0
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
TimerVal += 1
If TimerVal = 1 Then
Timer1.Enabled = False

MsgBox("started")
Timer1.Enabled = True
ElseIf TimerVal = 30 Then
Timer1.Enabled = False

MsgBox("ended")
Timer1.Enabled = True
TimerVal = 0
End If
End Sub
 
M

Miro

Actually you can put a sleep...

So instead of using a timer, and calling your Sub,
if you put something like this

Msgbox( "start" )

System.Threading.Thread.Sleep(30000) ' 30 seconds 1000 for every second.

MSBBox("END")

Your code will wait 30 seconds before it will continue.
So this might be the "Delay" you are looking for. Not a Timer.
 
S

Steve Lowe

Hi

Thanks for that -

I'm getting a Handles clause requires a WithEvents Variable error in
this line

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

^^^^^


and also a Timer1 is not declared error at all references to
Timer1.Enabled


Regards

Steve.


Make sure you set your timer interval to be a 1000 ( 1 second )
Make a variable to hold your "30" value.

And before you message box out, if you do not enable=false the timer, the
timer keeps counting.
Thats why its disabled and then enabled again after the fact.

Miro

Public TimerVal As Integer = 0
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
TimerVal += 1
If TimerVal = 1 Then
Timer1.Enabled = False

MsgBox("started")
Timer1.Enabled = True
ElseIf TimerVal = 30 Then
Timer1.Enabled = False

MsgBox("ended")
Timer1.Enabled = True
TimerVal = 0
End If
End Sub

- Steve Lowe
- E-Mail : (e-mail address removed)
- Before Replying Remove .NO.SPAM
- UK Resident although my e-mail address is usa.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top