locked
Set interval timer loop for aspx.vb RRS feed

  • Question

  • User-1974669539 posted

    Is there such thing call a Set interval timer loop for .vb programming?

    Which means every xx millisecond, it will execute any codes inside this loop?

    If yes, What is the code for it?

    Thanks in advance!

    Thursday, January 16, 2014 9:08 PM

Answers

  • User-1509636757 posted

    Try this way:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server" />
                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:Label ID="timerLabel" runat="server" />
                        <asp:Timer runat="server" ID="tmTimer" OnTick="tmTimer_Tick" Interval="1000"></asp:Timer>
                    </ContentTemplate>
                </asp:UpdatePanel>
    
            </div>
        </form>
    </body>
    </html>
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub tmTimer_Tick(sender As Object, e As EventArgs)
            timerLabel.Text = GenerateRandomString(10, False)
        End Sub
    End Class



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 19, 2014 12:26 AM

All replies

  • User-1509636757 posted

    You can use Timer control. Please check this example which executes Timer_Tick event every 1 second to update the label text to show current time:

    ASPX Code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server" />
                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:Label ID="lblGetDate" runat="server" />
                        <asp:Timer runat="server" ID="tmTimer" OnTick="tmTimer_Tick" Interval="1000"></asp:Timer>
                    </ContentTemplate>
                </asp:UpdatePanel>
    
            </div>
        </form>
    </body>
    </html>

    Code Behind:

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub tmTimer_Tick(sender As Object, e As EventArgs)
            lblGetDate.Text = DateTime.Now
        End Sub
    End Class

    hope it helps./.

    Thursday, January 16, 2014 11:21 PM
  • User-1974669539 posted

    Hi Kaushal. Thanks for reply.

    The moment when i drag and drop the AJAX Extension -> Timer into my aspx. There is error shown below.

    Error 1 Type 'System.Web.UI.Timer' is not defined. C:\inetpub\wwwroot\MMORPGapi\MMORPGapi\MMORPGapi\GameLogin.aspx.designer.vb 59 36 MMORPGapi

    I dunno what to import or change to? please advice

    Friday, January 17, 2014 9:34 PM
  • User-1509636757 posted

    As the first option says; Click on Import System.Timers to add that in namespaces. That should resolve the issue.

    Friday, January 17, 2014 11:18 PM
  • User-1974669539 posted

    Hi thanks. the error is fixed but the timer doesn't work at all.

    When I run my aspx, it says i need a script manager, so i just drag a script manager in.

    but still it doesn't work. 

    I tried to do a test by placing a random string generator method in the timer sub. So it should every second executethe method and display on timerLabel.Text but when I run my aspx, the label is empty.

    Protected Sub checkLoginTimer_Tick(sender As Object, e As EventArgs)
            timerLabel.Text = GenerateRandomString(10, False)
        End Sub

    <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="checkLoginTimer" runat="server" OnTick="checkLoginTimer_Tick" Interval="10000">
            </asp:Timer>
            <asp:Label ID="timerLabel" runat="server" Text="Label"></asp:Label>

    Please kindly assist me in detail as I am newbie. thanks

    Saturday, January 18, 2014 9:33 PM
  • User-1509636757 posted

    Try this way:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server" />
                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:Label ID="timerLabel" runat="server" />
                        <asp:Timer runat="server" ID="tmTimer" OnTick="tmTimer_Tick" Interval="1000"></asp:Timer>
                    </ContentTemplate>
                </asp:UpdatePanel>
    
            </div>
        </form>
    </body>
    </html>
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub tmTimer_Tick(sender As Object, e As EventArgs)
            timerLabel.Text = GenerateRandomString(10, False)
        End Sub
    End Class



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 19, 2014 12:26 AM