Timer Not Working - Can Anyone Explain Why and How to fix?

  • Thread starter Thread starter Sir Bill
  • Start date Start date
S

Sir Bill

I can not seem to get a system timer to work for me. Here is the code both
the aspx and aspx.vb file. The textbox displays "Timer Started" and never
anything else. What am I missing?


<%@ Page Language="VB" Debug="true" AutoEventWireup="false"
CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server">This is a text
box</asp:TextBox>
</div>
</form>
</body>
</html>
And the following codebehind file:
Imports System.Web.UI.WebControls
Partial Class test
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
CreateTimer()
Dim myTxt As WebControls.TextBox
myTxt = Form.FindControl("TextBox1")
myTxt.Text = "Timer Started"
End If
End Sub
Private Sub CreateTimer()
Dim Timer1 As New System.Timers.Timer
Timer1.Interval = 5000
Timer1.Enabled = True
AddHandler Timer1.Elapsed, New
System.Timers.ElapsedEventHandler(AddressOf Me.Timer1_elapsed)
End Sub
Private Sub Timer1_elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs)
Dim myText As WebControls.TextBox
myText = Form.FindControl("textbox1")
myText.Text = "Timer Popped " & Today.TimeOfDay.ToString
End Sub
End Class
 
Hello Bill,

This is caused the Timer you define on server side wouldn't worked on
controls on client side. When you execute following code on server:

CreateTimer()
Dim myTxt As WebControls.TextBox
myTxt = Form.FindControl("TextBox1")
myTxt.Text = "Timer Started"

The server will create a new thread to execute code in Timer and run
remained code right away:

Dim myTxt As WebControls.TextBox
myTxt = Form.FindControl("TextBox1")
myTxt.Text = "Timer Started"

Then it sent full response to client. Even the code in the new thread was
finished, it won't send response to client side and the controls on client
won't be updated. In ASP.NET, we cannot use Timer like a Windows Form
application.

Luke
 
OK, I want change the source of an image at intervals. How do I do that. I
thought a timer would do it.
 
I can not seem to get a system timer to work for me. Here is the code both
the aspx and aspx.vb file. The textbox displays "Timer Started" and never
anything else. What am I missing?

A web page tries to execute as quickly as possible, and then it's done
and gone. It's not like a Windows form, that sticks around, takes user
input, and waits for timers. It is born, it renders, it's gone...
 
Back
Top