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

Any reason for an enabled timer not to fire or count down?

P: n/a
I have a timer on a form. It isn't firing at all. I know that the timer is
enabled, and that the interval is low (4000, which should be 4 seconds). To
ensure the timer wasn't being inadvertantly reset I put some extra code in
the subs that enable and disable the timer. They fire as expected.

To test this I added a second timer with a 1 second interval. The event for
this time would output the enabled status of the first timer and its
interval value. I saw "TRUE 4000" appear each second.

Should the 4000 value decrease over time, or is this static with an internal
counter being used by the timer?

Since the timer is showing as enabled, why doesn't it fire after four
seconds?

Also, what is the bestest way to reset the interval? This is the sub I call
to reset my timeout timer.
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi there... The way the timer works is you set the interval to a value in
milliseconds, then after the time has elapsed, the "Tick" event is fired.

Then it waits another "Interval" milliseconds and fires "Tick"... and so on,
until it's set to disabled (or the application exits).

By default, I believe the Timer has .Enabled = False, so you set the
interval then enable the timer...

Timer1.Interval = 4000
Timer1.Enabled = True

Every 4 seconds the Tick event of the timer will fire.

If you only want it to fire once, then in the Tick event, simply set
Timer1.Enabled = False...

Public Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
DirectCast(sender, Timer).Enabled = False
End Sub

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Noozer" <po********@127.0.0.1> wrote in message
news:eR**************@TK2MSFTNGP11.phx.gbl...
I have a timer on a form. It isn't firing at all. I know that the timer is
enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly reset I put some extra code in
the subs that enable and disable the timer. They fire as expected.

To test this I added a second timer with a 1 second interval. The event for this time would output the enabled status of the first timer and its
interval value. I saw "TRUE 4000" appear each second.

Should the 4000 value decrease over time, or is this static with an internal counter being used by the timer?

Since the timer is showing as enabled, why doesn't it fire after four
seconds?

Also, what is the bestest way to reset the interval? This is the sub I call to reset my timeout timer.
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub

Nov 20 '05 #2

P: n/a
"Noozer" <po********@127.0.0.1> schrieb
I have a timer on a form. It isn't firing at all. I know that the
timer is enabled, and that the interval is low (4000, which should be
4 seconds). To ensure the timer wasn't being inadvertantly reset I
put some extra code in the subs that enable and disable the timer.
They fire as expected.

To test this I added a second timer with a 1 second interval. The
event for this time would output the enabled status of the first
timer and its interval value. I saw "TRUE 4000" appear each
second.

Should the 4000 value decrease over time, or is this static with an
internal counter being used by the timer?

Since the timer is showing as enabled, why doesn't it fire after
four seconds?

Also, what is the bestest way to reset the interval? This is the sub
I call to reset my timeout timer.
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub


How do declare the first (4 sec.) timer and how do you add the event
handler?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
"Noozer" <po********@127.0.0.1> schrieb
I have a timer on a form. It isn't firing at all. I know that the
timer is enabled, and that the interval is low (4000, which should be
4 seconds). To ensure the timer wasn't being inadvertantly reset I
put some extra code in the subs that enable and disable the timer.
They fire as expected.
How do declare the first (4 sec.) timer and how do you add the event
handler?


I've tried to include all the relevent code without the extra bits... Let me
know if it isn't sufficient. (Also let me know if you want me to post again
with wrapping disabled)

uTimeout has a value of 4 at the start of the program. The timeout timer
(timTimeout) is not enabled until the CONNECT sub is called and is
succesful.

'Open a connection to a server to the specified address and port.
Public Function Connect(ByVal Server As String, ByVal port As Int16) As
Boolean
If uConnected Then 'If we are already connected we should disconnect first
Disconnect()
End If
Try 'Error trap in case connection fails
client = New TcpClient(Server, port) 'Create connection
'Attach our "DoRead" sub to handle incoming data.
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Clear() 'Clear the display
MarkConnected() 'Set state of this control to connected (enable timeout
timer, set flags, etc.)
Return True 'No errors, so we were successful. Return TRUE.
Catch ex As Exception
'Error occurred while trying to connect
DisplaySystem("Error - '" & ex.Message & "'") 'Write error to console
MarkDisconnected() 'Mark this control as disconnected (disable timeout
timer, set flags, etc.)
Return False 'No connection so return FALSE
End Try
End Function

'This routine handles incoming data from our TCP "client" object
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer 'Number of bytes in buffer
Dim Data As String 'String read from buffer
Try 'Error trapping
BytesRead = client.GetStream.EndRead(ar) 'Get # of bytes available
If BytesRead < 1 Then 'If there is no data to read, we should assume
we're disconnected.
MarkDisconnected()
Exit Sub
End If
ResetTimeout() 'Reset the timeout timer since there is incoming data
'Convert incoming data to a string...
Data = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead)
DisplayIn(Data) 'Display incoming data to display
'Start a new asyncronous read into buffer
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Catch ex As Exception
'Error occured
DisplaySystem("Error - '" & ex.Message & "'") ' Display error
MarkDisconnected() 'Set as disconnected since there was an error.
End Try
End Sub

'Send data to remote connection, appending a CR/LF to data. Return TRUE if
successful
Public Function SendLn(ByVal data As String) As Boolean
Return Send(data & Chr(10) & Chr(13))
End Function

'Send data to remote connection. Return TRUE if successful
Public Function Send(ByVal data As String) As Boolean
Try
Dim writer As New IO.StreamWriter(client.GetStream) 'Declare stream
writer
writer.Write(data) 'Send data to stream
writer.Flush() 'Flush stream to remote connection
DisplayOut(data) 'Send outgoing data to display
ResetTimeout() 'Reset timeout timer
Return True 'Success! Return TRUE
Catch ex As Exception
'Error occurred. Not successful.
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'Assume disconnected because of error.
Return False 'Unsuccessful send. Return FALSE.
End Try
End Function

'Send incoming text to the display.
Private Sub DisplayIn(ByVal text As String)
rtbDisplay.SelectionColor = uInColor
rtbDisplay.SelectionFont = uInStyle
rtbDisplay.AppendText(text)
End Sub

'Send outgoing text to the display.
Private Sub DisplayOut(ByVal text As String)
rtbDisplay.SelectionColor = uOutColor
rtbDisplay.SelectionFont = uOutStyle
rtbDisplay.AppendText(text)
End Sub

'Send system text to the display.
Private Sub DisplaySystem(ByVal text As String)
rtbDisplay.SelectionColor = uSysColor
rtbDisplay.SelectionFont = uSysStyle
rtbDisplay.AppendText(text)
End Sub

'Send user text to the display. This is externally generated test.
Public Sub DisplayUser(ByVal text As String)
rtbDisplay.SelectionColor = uUserColor
rtbDisplay.SelectionFont = uUserStyle
rtbDisplay.AppendText(text)
End Sub

'Send user text to the display, followed by a carriage return.
Public Sub DisplayUserLine(ByVal text As String)
DisplayUser(text & Chr(13))
End Sub

'Close current connection. Return TRUE if close was normal.
Public Function Disconnect()
If uConnected = False Then
Return True 'We're already disconnected. Nothing to do.
End If
Try 'Error trap
client.Close() 'Try and close connection
client = Nothing 'Destroy our connection object
MarkDisconnected() 'Set state of this control to disconnected
Return True 'Close was normal. Return TRUE.
Catch ex As Exception
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'There was an error during disconnect. Assume
disconnect.
Return False 'Normal close did not occur. Return FALSE.
End Try
End Function

'Set this control to disconnected state
Private Sub MarkDisconnected()
If uConnected Then 'Only do this if we aren't already in a disconnected
state
uConnected = False
timTimeout.Enabled = False 'Turn off timeout timer
DisplaySystem("Disconnected" & Chr(13))
RaiseEvent StateChanged(False, Me)
End If
End Sub

'Set this control to a connected state
Private Sub MarkConnected()
If Not uConnected Then 'Only do this if we aren't already connected
uConnected = True
timTimeout.Interval = uTimeout * 1000 'Set timeout interval for
timeout timer
timTimeout.Enabled = True 'Enable the timeout
timer
DisplaySystem("Connected..." & Chr(13)) 'Send CONNECTED to display
RaiseEvent StateChanged(True, Me) 'Raise event to indicate state
change.
End If
End Sub

'Clear display area
Public Sub Clear()
rtbDisplay.Clear()
End Sub

'Timeout has fired. We are disconnecting.
Private Sub timTimeout_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles timTimeout.Tick
Disconnect()
DisplaySystem("Application timeout. Disconnected.")
End Sub

'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub
Nov 20 '05 #4

P: n/a
> "Armin Zingler" <az*******@freenet.de> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
"Noozer" <po********@127.0.0.1> schrieb
I have a timer on a form. It isn't firing at all. I know that the
timer is enabled, and that the interval is low (4000, which should be
4 seconds). To ensure the timer wasn't being inadvertantly reset I
put some extra code in the subs that enable and disable the timer.
They fire as expected.
How do declare the first (4 sec.) timer and how do you add the event
handler?


I've tried to include all the relevent code without the extra bits... Let

me know if it isn't sufficient.
Ever more than.. ;-)
(Also let me know if you want me to post again
with wrapping disabled)
No, thanks, after adding some declarations it is compilable.

Is it possible that you call sub ResetTimeout quite frequently?
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True

Debug.Writeline now & " ResetTimeout"
End Sub

--
Armin

Nov 20 '05 #5

P: n/a
.... I don't expect anyone to pick through this code and exclaim "Eureka!" I
was just hoping that someone may have some idea of a scenario that would
cause a timer not to fire.

The only thing I assume would stop a timer from firing is if it fired once
and never finished executing its code, but that isn't the case here.

A bit more info... I'm connecting to a POP3 server to test the control. I
connect to the server successfully and the welcome banner displays. There is
no more data from the remote connection and the application just sits, and
should time out after four seconds, but never does.
"Noozer" <po********@127.0.0.1> wrote in message
news:KnpGb.810051$6C4.754551@pd7tw1no...

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
"Noozer" <po********@127.0.0.1> schrieb
I have a timer on a form. It isn't firing at all. I know that the
timer is enabled, and that the interval is low (4000, which should be
4 seconds). To ensure the timer wasn't being inadvertantly reset I
put some extra code in the subs that enable and disable the timer.
They fire as expected.
How do declare the first (4 sec.) timer and how do you add the event
handler?


I've tried to include all the relevent code without the extra bits... Let

me know if it isn't sufficient. (Also let me know if you want me to post again with wrapping disabled)

uTimeout has a value of 4 at the start of the program. The timeout timer
(timTimeout) is not enabled until the CONNECT sub is called and is
succesful.

'Open a connection to a server to the specified address and port.
Public Function Connect(ByVal Server As String, ByVal port As Int16) As
Boolean
If uConnected Then 'If we are already connected we should disconnect first Disconnect()
End If
Try 'Error trap in case connection fails
client = New TcpClient(Server, port) 'Create connection
'Attach our "DoRead" sub to handle incoming data.
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Clear() 'Clear the display
MarkConnected() 'Set state of this control to connected (enable timeout
timer, set flags, etc.)
Return True 'No errors, so we were successful. Return TRUE.
Catch ex As Exception
'Error occurred while trying to connect
DisplaySystem("Error - '" & ex.Message & "'") 'Write error to console
MarkDisconnected() 'Mark this control as disconnected (disable timeout
timer, set flags, etc.)
Return False 'No connection so return FALSE
End Try
End Function

'This routine handles incoming data from our TCP "client" object
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim BytesRead As Integer 'Number of bytes in buffer
Dim Data As String 'String read from buffer
Try 'Error trapping
BytesRead = client.GetStream.EndRead(ar) 'Get # of bytes available
If BytesRead < 1 Then 'If there is no data to read, we should assume
we're disconnected.
MarkDisconnected()
Exit Sub
End If
ResetTimeout() 'Reset the timeout timer since there is incoming data
'Convert incoming data to a string...
Data = Encoding.ASCII.GetString(ReadBuffer, 0, BytesRead)
DisplayIn(Data) 'Display incoming data to display
'Start a new asyncronous read into buffer
client.GetStream.BeginRead(ReadBuffer, 0, ReadBufferSize, AddressOf
DoRead, Nothing)
Catch ex As Exception
'Error occured
DisplaySystem("Error - '" & ex.Message & "'") ' Display error
MarkDisconnected() 'Set as disconnected since there was an error.
End Try
End Sub

'Send data to remote connection, appending a CR/LF to data. Return TRUE if
successful
Public Function SendLn(ByVal data As String) As Boolean
Return Send(data & Chr(10) & Chr(13))
End Function

'Send data to remote connection. Return TRUE if successful
Public Function Send(ByVal data As String) As Boolean
Try
Dim writer As New IO.StreamWriter(client.GetStream) 'Declare stream
writer
writer.Write(data) 'Send data to stream
writer.Flush() 'Flush stream to remote connection
DisplayOut(data) 'Send outgoing data to display
ResetTimeout() 'Reset timeout timer
Return True 'Success! Return TRUE
Catch ex As Exception
'Error occurred. Not successful.
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'Assume disconnected because of error.
Return False 'Unsuccessful send. Return FALSE.
End Try
End Function

'Send incoming text to the display.
Private Sub DisplayIn(ByVal text As String)
rtbDisplay.SelectionColor = uInColor
rtbDisplay.SelectionFont = uInStyle
rtbDisplay.AppendText(text)
End Sub

'Send outgoing text to the display.
Private Sub DisplayOut(ByVal text As String)
rtbDisplay.SelectionColor = uOutColor
rtbDisplay.SelectionFont = uOutStyle
rtbDisplay.AppendText(text)
End Sub

'Send system text to the display.
Private Sub DisplaySystem(ByVal text As String)
rtbDisplay.SelectionColor = uSysColor
rtbDisplay.SelectionFont = uSysStyle
rtbDisplay.AppendText(text)
End Sub

'Send user text to the display. This is externally generated test.
Public Sub DisplayUser(ByVal text As String)
rtbDisplay.SelectionColor = uUserColor
rtbDisplay.SelectionFont = uUserStyle
rtbDisplay.AppendText(text)
End Sub

'Send user text to the display, followed by a carriage return.
Public Sub DisplayUserLine(ByVal text As String)
DisplayUser(text & Chr(13))
End Sub

'Close current connection. Return TRUE if close was normal.
Public Function Disconnect()
If uConnected = False Then
Return True 'We're already disconnected. Nothing to do.
End If
Try 'Error trap
client.Close() 'Try and close connection
client = Nothing 'Destroy our connection object
MarkDisconnected() 'Set state of this control to disconnected
Return True 'Close was normal. Return TRUE.
Catch ex As Exception
DisplaySystem("Error - '" & ex.Message & "'")
MarkDisconnected() 'There was an error during disconnect. Assume
disconnect.
Return False 'Normal close did not occur. Return FALSE.
End Try
End Function

'Set this control to disconnected state
Private Sub MarkDisconnected()
If uConnected Then 'Only do this if we aren't already in a disconnected
state
uConnected = False
timTimeout.Enabled = False 'Turn off timeout timer
DisplaySystem("Disconnected" & Chr(13))
RaiseEvent StateChanged(False, Me)
End If
End Sub

'Set this control to a connected state
Private Sub MarkConnected()
If Not uConnected Then 'Only do this if we aren't already connected
uConnected = True
timTimeout.Interval = uTimeout * 1000 'Set timeout interval for
timeout timer
timTimeout.Enabled = True 'Enable the timeout
timer
DisplaySystem("Connected..." & Chr(13)) 'Send CONNECTED to display
RaiseEvent StateChanged(True, Me) 'Raise event to indicate state change.
End If
End Sub

'Clear display area
Public Sub Clear()
rtbDisplay.Clear()
End Sub

'Timeout has fired. We are disconnecting.
Private Sub timTimeout_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles timTimeout.Tick
Disconnect()
DisplaySystem("Application timeout. Disconnected.")
End Sub

'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True
End Sub

Nov 20 '05 #6

P: n/a

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Armin Zingler" <az*******@freenet.de> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
"Noozer" <po********@127.0.0.1> schrieb
> I have a timer on a form. It isn't firing at all. I know that the
> timer is enabled, and that the interval is low (4000, which should be > 4 seconds). To ensure the timer wasn't being inadvertantly reset I
> put some extra code in the subs that enable and disable the timer.
> They fire as expected.
How do declare the first (4 sec.) timer and how do you add the event
handler?


I've tried to include all the relevent code without the extra bits... Let me
know if it isn't sufficient.


Ever more than.. ;-)
(Also let me know if you want me to post again
with wrapping disabled)


No, thanks, after adding some declarations it is compilable.

Is it possible that you call sub ResetTimeout quite frequently?


Yes... After each block of data from the connection as well as after sending
any data. Are there issues with how I'm trying to reset the timer? The code
below doesn't seem 'correct' to get the timer to restart to me.
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True

Debug.Writeline now & " ResetTimeout"
End Sub

Nov 20 '05 #7

P: n/a
"Noozer" <po********@127.0.0.1> schrieb
Is it possible that you call sub ResetTimeout quite frequently?


Yes... After each block of data from the connection as well as after
sending any data. Are there issues with how I'm trying to reset the
timer? The code below doesn't seem 'correct' to get the timer to
restart to me.
'Reset the timeout timer
Private Sub ResetTimeout()
timTimeout.Enabled = False
timTimeout.Interval = uTimeout * 1000
timTimeout.Enabled = True

Debug.Writeline now & " ResetTimeout"
End Sub

Well, if you restart the timer each 3 seconds, it will never fire if
interval = 4000. If this is not the case, I don't have a clue at current.
--
Armin

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.