7 Replies - 3672 Views - Last Post: 12 December 2012 - 02:45 PM Rate Topic: -----

#1 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 11:25 AM

I can't figure out why my tick event isn't firing. I've stepped through the code, watched it set Timer1.Enabled to true, but the event will not fire. The timer becomes enabled off a button click event.

Button click event:
 protected void btnAddToBag_Click(object sender, CommandEventArgs e)
        {

                    
                    Timer1.Enabled = true;
                    pnlWasAdded.Visible = true;
                    itemWasAddedImage.Visible = true;
                    txtQty.Text = "1";

                    
                    
                }
            }


Page_Load:
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnAddToBag.Click += new EventHandler(btnAddToBag_Click);

                Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);


        }


Timer Event to fire:
        void Timer1_Tick(object sender, EventArgs e)
        {
            
            pnlWasAdded.Visible = false;
            itemWasAddedImage.Visible = false;
            Timer1.Enabled = false;
            
        }

Mark Up:
<asp:UpdatePanel ID="pnlWasAdded" UpdateMode="Conditional" runat="server" Visible="false">
    <ContentTemplate>
        <asp:Image ImageUrl="/Images/ItemWasAdded.png" AlternateText="Item was added." Visible="false"
            ID="itemWasAddedImage" Style="margin-top: -35px; margin-left:-15px; z-index: 999;" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="2000" Enabled="false">
</asp:Timer>


While I'm debugging, Timer1.Enabled gets set to true as I step through the code. It never fires though.

<Scratching my head>

Is This A Good Question/Topic? 0
  • +

Replies To: Timer.Enabled running in debug mode, but not firing the event.

#2 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 784
  • Joined: 15-April 09

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 12:02 PM

Timer1.Start() might be your answer.
Was This Post Helpful? 0
  • +
  • -

#3 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 12:30 PM

View Postmagius96, on 11 December 2012 - 12:02 PM, said:

Timer1.Start() might be your answer.


From the research I've done, Enabled = true and Start() invoke the same call. I did also try this in my code with the same result.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,971
  • Joined: 02-June 10

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 01:10 PM

Enabled = true is the same as calling the Start() method. YOu're on track there.

If it gets started then it will fire the event. Simply has to be.

So the next step in debugging is figuring out why you aren't seeing it.

What is the .Interval of the timer?

I have no idea about ASP. But as for the C# the question becomes:
Are you really starting the timer? Does a breakpoint here get hit?
Are you sure you have subscribed? Does a breakpoint here get hit?
Are you sure it doesn't call the _Tick() hander? Does a breakpoint here get hit?
Was This Post Helpful? 0
  • +
  • -

#5 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 784
  • Joined: 15-April 09

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 02:43 PM

Remove the enabled="false" from the asp side of the code and add this to the page_load event:

if(!IsPostBack) Timer1.Enabled = false;
Was This Post Helpful? 1
  • +
  • -

#6 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 11 December 2012 - 03:14 PM

View Postmagius96, on 11 December 2012 - 02:43 PM, said:

Remove the enabled="false" from the asp side of the code and add this to the page_load event:

if(!IsPostBack) Timer1.Enabled = false;


That did the trick! Still baffled why my code would step through, mark it enabled and not run the code.

Thank you much. +rep
Was This Post Helpful? 0
  • +
  • -

#7 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 784
  • Joined: 15-April 09

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 12 December 2012 - 08:40 AM

Control flow. Anytime the backend code is initiated, the front end code is redrawn. Since you had the enabled=false in the front end code, that over-wrote what was done previously in the backend causing your timer to never be enabled. By moving the enabled setting to the page_load and having it only run when it's not a postback, you are setting it once an letting it ride thereafter.
Was This Post Helpful? 3
  • +
  • -

#8 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Re: Timer.Enabled running in debug mode, but not firing the event.

Posted 12 December 2012 - 02:45 PM

Perfect. Thanks for the explanation. I will keep this in mind while I continue to develop. :) One of those things you just don't know about until you come across it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1