473,392 Members | 1,276 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,392 software developers and data experts.

Getting a timer to work

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
11 2560
"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
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
"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
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
"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
LOL ---

So back to the original question --- did the original poster ever get his
issue resolved?
Aug 22 '07 #7
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
6
by: Jim H | last post by:
I have a class object that creates and uses a System.Threading.Timer. In the destructor for my class I cancel the timer using timer.Change(infinite, infinite) then I call timer.Dispose() to clean...
2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
4
by: Rich P | last post by:
Greetings, I have a routine I was running in VB6 on a timed schedule. When the timeframe came up, the timer would be disable, the routine would run, and the timer gets enabled. I am trying to...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
3
by: Dave | last post by:
Hey all, I have a problem and I can't find anything on the net to really address it. We have a program at work that has lots of numbers on the front of the form because it is a control program...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
7
by: Brian | last post by:
hello Gentlemen, been trying to get a shell program working where I have a Windows service that also has a Notification icon in the system tray..... I got this idea from microsofts Visual...
2
by: Rico | last post by:
Hello, I'm trying run a process at regular intervals using a windows service. the problem is, the timer_tick event doesn't appear to be working. I've written the following code as a test; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.