显示消息框时如何继续计时? [英] how to continue timer when a message box is shown?

查看:114
本文介绍了显示消息框时如何继续计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我在应用程序中使用计时器,该计时器负责倒数计时器.

当计时器倒计时时,我在倒计时期间设置了一个带有单个OK botton的消息框.
这是弹出消息框时计时器暂时停止的问题!当按下确定"按钮且消息框消失时,计时器继续计时.


解决方案

此行为不正常,您在说什么定时器? />
这段代码有效:

 公共 部分  class  Form1:表格
{
    计时器计时器
     int 计数器=  0 ;

    公共 Form1()
    {
        InitializeComponent();

        timer =  Timer();
        计时器.滴答+ =  EventHandler(timer_Tick);
        timer.Interval =  1000 ;
        timer.Enabled =  true ;
    }

    无效 timer_Tick(对象发​​送者,EventArgs e)
    {
        计数器++;
        label1.Text = counter.ToString();
    }

    私有 无效 button1_Click(对象发​​件人,EventArgs e)
    {
        // 即使显示了消息框
        // 计时器仍在计时并且标签已更新
        MessageBox.Show(" );
    }
} 



您正在使用哪种计时器?


使用其他计时器类!


我没有找到问题的所有细节,但是我确信可以通过使用System.Timers.Timer不是 System.Windows.Forms.Timer来解决.最后一个计时器类更易于使用,但出了名的问题.我只遇到了其他开发人员带来的几个问题(准确性差,导致该计时器不适合动画);并替换计时器类解决了所有问题.


如果需要使用事件System.Timers.Timer.Elapsed的处理程序中的UI控件.应该使用正确的同步抛出Control.InvokeRequiredControl.Invoke.

—SA


问题在于MessageBox是模态对话框.
这意味着在用户响应消息之前,将暂停父窗体中的所有处理,然后单击确定"或取消"按钮(例如),关闭对话框.

这意味着计时器Tick事件不会得到处理.

最好的解决方法是从常规WinForm创建自己的MessageBox,并使用Form.Show方法而不是Form.ShowDialog(与MessageBox.Show方法等效)显示该消息.


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?

解决方案

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

This code works:

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?


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


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).


这篇关于显示消息框时如何继续计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆