2

i wanted to use a timer in a program i'm working on, but it always stops after 1 tick !! can you give me any tips to make it repeat unstoppably (or until i want it to) please ??
this is my code:

    int i = 20;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "(20)";
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        i--;
        Label1.Text = "(" + i + ")";

        if (i == 0)
        {
            Session["Profession"] = "Visiteur";
            Response.Redirect("Acceuil.aspx");
        }

Edit: my HTML code :

<form id="form1" runat="server">
<div><center>
    <span class="style1">message</span><br 
        class="style1" />

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" >
    </asp:Timer>

        <asp:Timer ID="Timer2" runat="server" Interval="1000">
    </asp:Timer>

        <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
        PostBackUrl="~/Acceuil.aspx">Retour</asp:LinkButton>
    <br />
        </center>
</div>
</form>

Edit2: what i want to do is the normal redirecting code, count to 20 and refreshing every second (to show to user how much secs left) and at the end it redirects to a new page, but it wasn't working, so i was wondering why ?? and i think i got my answer from @Andrei Rînea, and thank you everyone for your help : )

Edit3: Solved and here is the code :

static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    Label1.Text = "20";
}
protected void Timer1_Tick(object sender, EventArgs e)
{

    i--;
    Label1.Text = i.ToString();

    if (i == 0)
    {
        i = 20;
        Session["Profession"] = "Visiteur";
        Response.Redirect("Acceuil.aspx");
    }
}

Thanks everyone again : )

Tonner Mààn
  • 133
  • 1
  • 11
  • 1
    I can't tell for sure without seeing your timer, but the one time I've seen a similar issue, it was because the timer was garbage collected. Incidentally, do you run the code in Debug mode, or Release mode? – Liz Apr 16 '15 at 11:04
  • what are you trying todo with this? is it the timer as in http://www.asp.net/AJAX/Documentation/Live/tutorials/TimerControlWithUpdatePanelsTutorial.aspx – Steve Drake Apr 16 '15 at 11:19
  • i saw that link already before posting here, even with that same methode it only do it once, and don't go further. with my program, it writes : (20) after 1 sec it change to (19) and then it don't change anymore !! : / – Tonner Mààn Apr 16 '15 at 13:16

3 Answers3

1

I'm going to assume here that you are wishing to have a process that ticks away on your server in the background.

I'm also going to assume you've tried to add this to your Webform.

If so, the issue you have encountered, is that your Webform object only exists for the short time that it is processing your request, after which it is disposed of - including your timer.

If I'm correct, you'd probably like to take a look at Quartz.Net:

http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net

Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • Technical this will work eg it will do something every x, but IMHO I would consider this a bad pattern. What if you scale out? what if you use web gardens? I would consider using a scheduled task to schedule a task, eg windows task scheduler. Also if you look at the Q its got a redirect that's not going to work with Quartz.net – Steve Drake Apr 16 '15 at 11:16
  • Of course @SteveDrake is correct - you need to be careful with this kind of thing. – Dave Bish Apr 16 '15 at 11:18
  • I think it relates to this timer, eg AJAXey thing http://www.asp.net/AJAX/Documentation/Live/tutorials/TimerControlWithUpdatePanelsTutorial.aspx – Steve Drake Apr 16 '15 at 11:20
  • ironically, i needed todo a background task about 1 hour ago (I took my advice about scheduled tasks etc), but my job is a tidy up job so it fits well. While looking I found this https://blog.mariusschulz.com/2014/05/07/scheduling-background-jobs-from-an-asp-net-application-in-net-4-5-2 built into .net as of 4.5.2 :) thought that might be of interested – Steve Drake Apr 16 '15 at 13:47
  • @SteveDrake it's not really what i need but still usefull, i may (mostly will) need it, thank you very much : ) – Tonner Mààn Apr 16 '15 at 13:54
  • I thought it not what you needed but it felt on topic. Your welcome. – Steve Drake Apr 16 '15 at 14:00
1
Please use the following code :



    static int i = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        Label1.Text = "(20)";
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

        i--;
        Label1.Text =i.ToString();

        if (i == 0)
        {
            Session["Profession"] = "Visiteur";
            Response.Redirect("Acceuil.aspx");
        }
    }


    your HTML code should be like :

    <form id="form1" runat="server">
        <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>


            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>



                 <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
                   </asp:Timer>
                </ContentTemplate>

            </asp:UpdatePanel>

        </div>
        </form>
Ravin
  • 61
  • 4
  • the html code is simillar, i added it to my main post, i'll try your code and give you a feed back : ) – Tonner Mààn Apr 16 '15 at 13:36
  • didn't work, the page is refreshing every second but the number stays at 19 : / i tried your code and this code too in the page load : if (!Page.IsPostBack) { Label1.Text = "(20)"; Timer1.Tick += new EventHandler(Timer1_Tick); } – Tonner Mààn Apr 16 '15 at 13:44
  • well this is working fine at my end.Please make the variable 'i' as static . – Ravin Apr 16 '15 at 15:35
  • Wohaa like magic it worked XD i had 1 problem tho, and it's that if i run it a second time it continues from where is was last time, so after the first test, it was doing nigatif numbers lol to fix that i puted in Timer1_Tick after the the condition if and before Session["Profession"] this : i = 20; problem resolved \o/ – Tonner Mààn Apr 16 '15 at 16:28
0

You are trying to run client side code on the server. You need to do this in JavaScript instead.

The C# code will be run just before rendering the page and not longer. You probably believe the code will run as long as the visitor is on the page which is not the case.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166