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

Discussion in 'ASP .Net' started by Sir Bill, Jan 11, 2006.

  1. Sir Bill

    Sir Bill Guest

    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
     
    Sir Bill, Jan 11, 2006
    #1
    1. Advertising

  2. 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
     
    Luke Zhang [MSFT], Jan 12, 2006
    #2
    1. Advertising

  3. Sir Bill

    Sir Bill Guest

    OK, I want change the source of an image at intervals. How do I do that. I
    thought a timer would do it.


    "Luke Zhang [MSFT]" <> wrote in message
    news:...
    > 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
    >
    >
     
    Sir Bill, Jan 12, 2006
    #3
  4. Sir Bill

    Scott Allen Guest

    On Wed, 11 Jan 2006 17:58:53 -0600, "Sir Bill"
    <> wrote:

    >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...

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/
     
    Scott Allen, Jan 12, 2006
    #4
  5. A quick and easy solution is a GIF image with multiple frame instead.

    Luke
     
    Luke Zhang [MSFT], Jan 12, 2006
    #5
  6. Sir Bill

    Scott Allen Guest

    Scott Allen, Jan 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    22
    Views:
    1,138
    Tim Roberts
    Mar 21, 2006
  2. Xah Lee
    Replies:
    23
    Views:
    1,084
    Tim Roberts
    Mar 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,962
    Smokey Grindel
    Dec 2, 2006
  4. pietaster
    Replies:
    1
    Views:
    151
    Bernard O'Flynn
    Oct 28, 2003
  5. Xah Lee
    Replies:
    21
    Views:
    803
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page