Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hi all,
i'm using a timer in my application which is responsible for a countdown timer.

while the timer is counting down, i've set a messagebox with a single OK botton to be shown during this countdown.
here is the problem that the timer stops temporarily when the messagebox popsup! And the timer continues when the OK botton is pressed and messagebox disappears.

how can i make timer to continue counting down while the messagebox is not closed yet?
Posted

This behaviour is not normal, what timer are you talking about??

This code works:

C#
public partial class Form1 : Form
{
    Timer timer;
    int counter = 0;

    public Form1()
    {
        InitializeComponent();

        timer = new Timer();
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 1000;
        timer.Enabled = true;
    }

    void timer_Tick(object sender, EventArgs e)
    {
        counter++;
        label1.Text = counter.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //even though the message box is displayed
        //the timer still counts and the label is updated
        MessageBox.Show("hi");
    }
}


What kind of timer are you using?
 
Share this answer
 
Comments
JOAT-MON 3-Feb-11 15:02pm    
This example is not a reproduction of the OP situation. The OP has the MessageBox.Show() in the timer_Tick() handler. To get a better sense of the issue, move the MessageBox.Show() into the timer_Tick and see if you encounter a problem with the Timer continuing while the MessageBox is displayed.
Olivier Levrey 3-Feb-11 15:22pm    
Then this would lead to hundreds of message boxes displaying at every tick... the messsage queue is not stopped by modal boxes! I tested it, and one box is displayed every second even if I don't close the previous one...
JOAT-MON 3-Feb-11 16:52pm    
Just tested it, even if you assign an Owner to it or wait on the MessageBox response the timer will still march on. (using the System.Windows.Forms.Timer VS 2010). OP must be using a different timer, or something else not indicated in question, that is causing the issue. Looks like he likes the workaround provided by OriginalGriff, though.
Sergey Alexandrovich Kryukov 3-Feb-11 21:47pm    
You code looks all right, but this timer itself is notoriously bad; I propose using System.Timers.Timer in all cases -- see my answer.
--SA
zaeemi 4-Feb-11 1:47am    
i'm about to turn off a device by reaching 2 results,
1. reaching the specified time that user defines in the timer!
2. reaching a value that is above the maximum line.
some other values exist that might take effect. these other values prompt user by a message box, here is the problem! this messagebox stops the countdown timer till the user press any button of that!

in addition it'd be better to mention that i'm using different timers for this action.
1. for recieving data from a COM port with the interval of 700ms.
2. one with the Interval of 1000 ms that does the "i--" or "i++" as Second,Minute,Hour counter.

Timer No.2 stops counting....
i wish that is much more clear now.

and good to mention that i'm using the timer component, i drag and drop it in my form. sorry if i'm not as pro as you guys....
thanks in advance every one......
Use different timer class!


I did not find out all the detail of the problem, but I'm sure it can be resolved by using System.Timers.Timer, not System.Windows.Forms.Timer. The last timer class is easier to use but notoriously problematic. I faced only a couple of problems created by other developers (poor accuracy that renders this timer no suitable for animation); and replacement of the timer class resolved all of the problem.


If you need to work with UI control from the handler of the event System.Timers.Timer.Elapsed. Proper synchronization should be used throw Control.InvokeRequired, Control.Invoke.

—SA
 
Share this answer
 
v2
Comments
Olivier Levrey 4-Feb-11 4:09am    
My 5. True: System.Windows.Forms timers are easy to use but shouldn't be used if accuracy in required.
The problem is that MessageBox is a Modal dialog.
What that means is that all processing in the parent form is suspended until such time as the user responds to the message, and clicks either the OK or Cancel buttons (for example), closing the dialog.

This means that the timer Tick events do not get processed.

The best way round this, is to create your own MessageBox from a regular WinForm, and display that using the Form.Show method, rather than the Form.ShowDialog (which is the equivalent of the MessageBox.Show method).
 
Share this answer
 
Comments
fjdiewornncalwe 3-Feb-11 14:16pm    
+5. Exactly what I would do as well.
Olivier Levrey 3-Feb-11 15:20pm    
This is not correct!! The message processing is not stopped by a modal dialog box! How would it be repainted properly?? The code I pasted below works well.
Sergey Alexandrovich Kryukov 3-Feb-11 21:47pm    
You code looks all right, but this timer itself is notoriously bad; I propose using System.Timers.Timer in all cases -- see my answer.
--SA
Buildfing on OriginalGriff's reply, I would put the timer into the new form class and have it dismiss itself when the countdown reaches zero (if I understand the intended nature of message box form).
 
Share this answer
 

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