By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,341 Members | 1,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,341 IT Pros & Developers. It's quick & easy.

Getting a timer to work

P: n/a
I'm quite new to programming but I'm having problems getting a timer
to work in visual studio.net

I've created a timer on a form, enabled it and then typed the
following code (from the mdsn library as I thought this would be a
good start!!!) but nothing happens :-
Imports System.Timers
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim configurationAppSettings As
System.Configuration.AppSettingsReader = New
System.Configuration.AppSettingsReader
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
'
'Timer1
'
Me.Timer1.Enabled =
CType(configurationAppSettings.GetValue("Timer1.En abled",
GetType(System.Boolean)), Boolean)
Me.Timer1.Interval =
CType(configurationAppSettings.GetValue("Timer1.In terval",
GetType(System.Int32)), Integer)
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
End Class
Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = 2000
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()
GC.KeepAlive(aTimer)
End Sub
Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e
As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
Any help would be greatly appreciated.
Thanks
Paul

Aug 21 '07 #1
Share this Question
Share on Google+
11 Replies

P: n/a
"Hotrod2000" <pb******@bmselland.comschrieb
I'm quite new to programming but I'm having problems getting a timer
to work in visual studio.net

I've created a timer on a form, enabled it and then typed the
following code (from the mdsn library as I thought this would be a
good start!!!) but nothing happens :-

- Did you set the sub main below as the startup object?
- The Timer in the Form has no purpose because the Form is never shown.
- Is the project type "console application"? Othwerwise you don't see the
output in the console (because you don't see the console).
Armin

Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = 2000
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()
GC.KeepAlive(aTimer)
End Sub
Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e
As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class

Aug 21 '07 #2

P: n/a
From your code, it looks like you're attempting to attach to a Elapsed
event, which doesn't exist.

Try changing the line:
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

To:
AddHandler aTimer.Tick, AddressOf OnTimedEvent

I'm surprised the VS will even compile the project without errors. Out of
curiosity, do you have the msdn link that you mentioned?
Aug 22 '07 #3

P: n/a
"Matt F" <mf****************@nospam.nospamschrieb
From your code, it looks like you're attempting to attach to a
Elapsed event, which doesn't exist.

Try changing the line:
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

To:
AddHandler aTimer.Tick, AddressOf OnTimedEvent

I'm surprised the VS will even compile the project without errors.
Out of curiosity, do you have the msdn link that you mentioned?
The type of aTimer is System.Timers.Timer which does have an Elapsed event.
Armin
Aug 22 '07 #4

P: n/a
your right --- sorry for the post --- this is a good examply of why I
shouldn't be posting stuff at 2:20 AM
Aug 22 '07 #5

P: n/a
"Matt F" <mf****************@nospam.nospamschrieb
your right --- sorry for the post --- this is a good examply of why
I shouldn't be posting stuff at 2:20 AM
But it was 11:20 AM! ;-)
Armin
Aug 22 '07 #6

P: n/a
LOL ---

So back to the original question --- did the original poster ever get his
issue resolved?
Aug 22 '07 #7

P: n/a
"Matt F" <mf****************@nospam.nospamschrieb
LOL ---

So back to the original question --- did the original poster ever
get his issue resolved?

Wait til 11:20 tomorrow - or til the Timer ticks. :-D
Armin
Aug 22 '07 #8

P: n/a
On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Matt F" <mfielderREMOVEC...@nospam.nospamschrieb
LOL ---
So back to the original question --- did the original poster ever
get his issue resolved?

Wait til 11:20 tomorrow - or til the Timer ticks. :-D

Armin
Thanks for all your posts...
I'm still struggling though...
All I'm trying to do is open a form with a few buttons on, (which
action different events, not a problem with that) but at the same time
start a timer which puts a message up if no button is pressed, but if
one of the buttons are pressed, the timer is reset.
I was hoping that by using a timer on the form I would just be able to
call it start and stop it when an action was performed.
Am I even using the correct timer ? as I understand there are three
different timers, windows.forms.timers.timer, system.timers and a
threading timer...
Having looked at what I'm doing I seem to using a mixture !!!(Not by
design) of the windows.form.timers.timer and the
system.timers.timer !!!
Where do I actually put the code for this ? is it on the form, or is
it in it's own timer class ?

Aug 23 '07 #9

P: n/a
On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmselland.comwrote:
On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Matt F" <mfielderREMOVEC...@nospam.nospamschrieb
LOL ---
So back to the original question --- did the original poster ever
get his issue resolved?
Wait til 11:20 tomorrow - or til the Timer ticks. :-D
Armin

Thanks for all your posts...
I'm still struggling though...
All I'm trying to do is open a form with a few buttons on, (which
action different events, not a problem with that) but at the same time
start a timer which puts a message up if no button is pressed, but if
one of the buttons are pressed, the timer is reset.
I was hoping that by using a timer on the form I would just be able to
call it start and stop it when an action was performed.
Am I even using the correct timer ? as I understand there are three
different timers, windows.forms.timers.timer, system.timers and a
threading timer...
Having looked at what I'm doing I seem to using a mixture !!!(Not by
design) of the windows.form.timers.timer and the
system.timers.timer !!!
Where do I actually put the code for this ? is it on the form, or is
it in it's own timer class ?
P.S. the link to the msdn page I got my original code from is :-
http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx

Thanks
Paul

Aug 23 '07 #10

P: n/a
"Hotrod2000" <pb******@bmselland.comschrieb
On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmselland.comwrote:
On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Matt F" <mfielderREMOVEC...@nospam.nospamschrieb
LOL ---
So back to the original question --- did the original poster
ever get his issue resolved?
Wait til 11:20 tomorrow - or til the Timer ticks. :-D
Armin
Thanks for all your posts...
I'm still struggling though...
All I'm trying to do is open a form with a few buttons on, (which
action different events, not a problem with that) but at the same
time start a timer which puts a message up if no button is
pressed, but if one of the buttons are pressed, the timer is
reset.
I was hoping that by using a timer on the form I would just be
able to call it start and stop it when an action was performed.
Am I even using the correct timer ? as I understand there are
three different timers, windows.forms.timers.timer, system.timers
and a threading timer...
Having looked at what I'm doing I seem to using a mixture !!!(Not
by design) of the windows.form.timers.timer and the
system.timers.timer !!!
Where do I actually put the code for this ? is it on the form, or
is it in it's own timer class ?

P.S. the link to the msdn page I got my original code from is :-
http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx
First, which timer to use:
http://msdn2.microsoft.com/en-us/lib...e6(VS.80).aspx
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
Second, did you read my first post? I'm gonna quote:

"- Did you set the sub main below as the startup object?
- The Timer in the Form has no purpose because the Form is never shown.
- Is the project type "console application"? Othwerwise you don't see the
output in the console (because you don't see the console)."
So, you must decide whether you want a Console application or a
WindowsApplication. Set it in the project properties under "application
type". Maybe it's possible to mix it, but that's currently not the point.

Now,,, in any application you have a Sub Main, the starting point of every
appliation. In a WindowsApplication, you can decide whether you a) specify a
Form as the startup object (in the project properteis), or b) write Sub Main
on your own. If you go for a), VB generates the invisible Sub Main which
mainly contains showing the startup Form. If you go for b), you are free in
if and when to show which Form.

If you change the application type to "console application" and set the Sub
Main that you posted as the "startup object", the timer should work.

If you want to test the Windows.Forms.Timer, you must set the application
type to "Windows application" and set the Form as the startup object. In
addition, you must add an event handler to the Timer's Tick event - the
Timer on the Form (by double-clicking the Timer icon on the designer).
Be aware that, now, it doesn't make sense anymore to use Console.Writline
because there is no console window in a "Windows application". You can
always write to the debug output using Debug.Writeline.
Armin

Aug 23 '07 #11

P: n/a
On 23 Aug, 16:30, "Armin Zingler" <az.nos...@freenet.dewrote:
"Hotrod2000" <pbrun...@bmselland.comschrieb


On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmselland.comwrote:
On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Matt F" <mfielderREMOVEC...@nospam.nospamschrieb
LOL ---
So back to the original question --- did the original poster
ever get his issue resolved?
Wait til 11:20 tomorrow - or til the Timer ticks. :-D
Armin
Thanks for all your posts...
I'm still struggling though...
All I'm trying to do is open a form with a few buttons on, (which
action different events, not a problem with that) but at the same
time start a timer which puts a message up if no button is
pressed, but if one of the buttons are pressed, the timer is
reset.
I was hoping that by using a timer on the form I would just be
able to call it start and stop it when an action was performed.
Am I even using the correct timer ? as I understand there are
three different timers, windows.forms.timers.timer, system.timers
and a threading timer...
Having looked at what I'm doing I seem to using a mixture !!!(Not
by design) of the windows.form.timers.timer and the
system.timers.timer !!!
Where do I actually put the code for this ? is it on the form, or
is it in it's own timer class ?
P.S. the link to the msdn page I got my original code from is :-
http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx

First, which timer to use:http://msdn2.microsoft.com/en-us/lib...T/default.aspx

Second, did you read my first post? I'm gonna quote:

"- Did you set the sub main below as the startup object?
- The Timer in the Form has no purpose because the Form is never shown.
- Is the project type "console application"? Othwerwise you don't see the
output in the console (because you don't see the console)."

So, you must decide whether you want a Console application or a
WindowsApplication. Set it in the project properties under "application
type". Maybe it's possible to mix it, but that's currently not the point.

Now,,, in any application you have a Sub Main, the starting point of every
appliation. In a WindowsApplication, you can decide whether you a) specify a
Form as the startup object (in the project properteis), or b) write Sub Main
on your own. If you go for a), VB generates the invisible Sub Main which
mainly contains showing the startup Form. If you go for b), you are free in
if and when to show which Form.

If you change the application type to "console application" and set the Sub
Main that you posted as the "startup object", the timer should work.

If you want to test the Windows.Forms.Timer, you must set the application
type to "Windows application" and set the Form as the startup object. In
addition, you must add an event handler to the Timer's Tick event - the
Timer on the Form (by double-clicking the Timer icon on the designer).
Be aware that, now, it doesn't make sense anymore to use Console.Writline
because there is no console window in a "Windows application". You can
always write to the debug output using Debug.Writeline.

Armin- Hide quoted text -

- Show quoted text -
Hi Armin,

Thanks for all your help. I'll be more careful in the future coping
code from web-sites as I didn't realise that the original code I
posted was for a console !! until you helped me to get it working !!!

I only ever intended to use my timer on a windows form.
Anyway with your help I've managed to get it working as I intended and
I've posted my code below which works fine for what I wanted ...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Timer1.Enabled = False
Dim f1 As New Form2
f1.Show()
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
AddHandler f1.Closed, New EventHandler(AddressOf
OnForm2Closed)
f1.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Timer1.Enabled = False
Dim f1 As New Form3
f1.Show()
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
AddHandler f1.Closed, New EventHandler(AddressOf
OnForm3Closed)
f1.Show()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click
Application.Exit()
End Sub
Private Sub OnForm2Closed(ByVal sender As Object, ByVal e As
EventArgs)
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Timer1.Enabled = True

End Sub
Private Sub OnForm3Closed(ByVal sender As Object, ByVal e As
EventArgs)
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Timer1.Enabled = True

End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Form1.ActiveForm.Opacity = 0.5
Timer1.Stop()
MessageBox.Show("press a button then...")
Form1.ActiveForm.Opacity = 100
Timer1.Start()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Start()
Timer1.Interval = 2000

End Sub

Thanks again...
Paul

Aug 24 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.