Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using a timer in my application to update progressbar. I have a button to start the timer.
C#
private void btnLogin_Click(object sender, EventArgs e)
 {
            timer1.Start();
 }


I have timer interval set to 1000. So, it should tick after every 1000 msec. I have some code to be executed each time the timer ticks. But the problem is, timer never ticks even after clicking the button.

As far as I know, if I start the timer, then it should call the timer.Tick() after the interval.
Posted

If you are using - 'System.Timers.Timer', make sure the Timer's Enabled property is set to true.
 
Share this answer
 
v2
Comments
André Kraak 9-Oct-11 8:50am    
If I read the documentation correctly Timer.Start() and Timer.Enabled=true do the same thing.
Abhinav S 9-Oct-11 8:53am    
Not sure. Will Timer.Start() work if the Timer is disabled?
Setting Timer.Enabled = true will start the timer and you are correct in that sense.
André Kraak 9-Oct-11 8:59am    
I think so.

Timer.Enabled is false by default and it's documentation (http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled.aspx) says in the remark section "Setting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.".
Jyoti Choudhury 9-Oct-11 9:13am    
Thanks for your answers. my problem has been solved.
How did you set-up the timer[^], it should look like:
C#
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

and the timer function should look like:
C#
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
}
 
Share this answer
 
v3
I think this will solve ur Problem :-
Jst Read This what r going here .....
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script  runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

        List<string> Quote = new List<string>();
        Quote.Add("A");
        Quote.Add("B");
        Quote.Add("C");

        Random rnd = new Random();
        lblQuote.Text = Quote[rnd.Next(Quote.Count)];
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Timer Example</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"/>
        <asp:Timer ID="Timer1" Interval="2000" runat="server"/> 
        <fieldset>
        <legend>Quotes</legend>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </triggers>
            <contenttemplate>
            <asp:Label ID="lblQuote" runat="server" />
            </contenttemplate>
            
        </fieldset>
    </div>
    </form>
</body>
</html></string></string>
 
Share this answer
 
v2
Comments
RaviRanjanKr 9-Oct-11 9:54am    
Minor Edited in your answer. just used "Pre" tag to wrap your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900