Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I want to have a timer that is run after pushing a button.
after a predefined time it should set a bit.
In the button procedure I wait for that bit.but it freezes in the "while"'!!!!
C#
private void btnStart_Click(object sender, EventArgs e)
   {
       timer1.Interval = 100;
       TimerEnd = false;
       label1.Text = "Process Started...";
       timer1.Enabled = true;
       while (!TimerEnd) ;
       label1.Text = "Process Finished";

   }

   private void timer1_Tick(object sender, EventArgs e)
   {
       counter++;
       if (counter > 10)
       {
           TimerEnd = true;
           counter = 0;
       }

   }
Posted
Updated 8-Dec-19 5:44am
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 17:28pm    
The exact type of the timer please. It's critically important. Isn't it System.Windows.Forms.Timer? System.Timers.Timer?
--SA

    private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Interval = 100;
Add ->      timer1.Tick += timer1_Tick
            TimerEnd = false;
            label1.Text = "Process Started...";
            timer1.Enabled = true;
            while (!TimerEnd) ; 
            label1.Text = "Process Finished";
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            if (counter > 10)
            {
                TimerEnd = true;
                counter = 0;
            }      
        }

You never set the event handler.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 23:57pm    
It's possible, but you cannot state that for sure; may be OP did not show it.
--SA
Reza29ir 17-Aug-12 5:54am    
Dear Sergey
As you know in Form1.Designer.cs we have
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
so I have done this!!
how abt this. this is what you are looking i guess.
System.Timers.Timer time = new System.Timers.Timer(100);
time.Elapsed += Time_Elapsed;


private static void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
label1.Text = "Process Finished";
}
 
Share this answer
 
Comments
Dave Kreskowiak 8-Dec-19 11:45am    
Try reading the other answers along with the question, and looking at the date on the question before answering. This one is 7 years old. I seriously doubt the OP is still looking for an answer.
Ashutosh Gpta 8-Dec-19 11:59am    
why do you think so that is only for OP not for others in future until and unless they get the right answer. anyway i didnt realize the date but this question was appearing on top of list to answer. down vote for answering without noticing date of question asked, anyways i dont mind of down vote, it is going to help some one in future for sure.
Dave Kreskowiak 8-Dec-19 14:02pm    
Because your answer was nothing more than a stripped down version of Solution 2 with no explanation of what the OP missed doing.
Hi, Just replace
while (!TimerEnd) ;

with
while (!TimerEnd)
{
    Application.DoEvents();
}
 
Share this answer
 
Comments
Dave Kreskowiak 8-Dec-19 10:36am    
no, No, NO. Application.DoEvents() is a bad solution to anything. If you're using it, you're doing threading, or something else, very wrong. DoEvents introduces problems in your code if you don't have any idea what it does behind the scenes. One example is event handlers that shouldn't be started again if it is already running.

The OP's question, from 7 YEARS AGO, was due to a poor understanding of how threading works and, today, the pattern he demonstrated would be better handled with Tasks.
hgh6484 11-Jan-22 10:43am    
Dear Dave!
It's not the best answer, I just notify how that code can run and debug.
Dave Kreskowiak 11-Jan-22 10:55am    
Telling someone to use DoEvents when THEY don't know what it does or how it works is an even worse idea.

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