c# - C# WinForm 中 MessageBox 的连续弹出

标签 c# winforms messagebox

这是我的代码:

private void btnReminder_Click(object sender, EventArgs e)
{
    timer2.Start();
}

private void timer2_Tick(object sender, EventArgs e)
{
    DateTime Date = DateTimePickerReminderDate.Value;
    DateTime Time = DateTimePickerReminderTime.Value;
    if (DateTime.Now.CompareTo(Date) > 0 && DateTime.Now.CompareTo(Time) > 0) 
    {
        Date = DateTime.MaxValue;
        Time = DateTime.MaxValue; 
        MessageBox.Show("Your Reminder");

        timer2.Stop();     
    }
}   

当我设置提醒时它按预期工作并在正确的时间显示消息。但问题是,它不断弹出消息框我试图清除这个错误但我没有成功。所以现在我需要专家的建议,所以请帮我清除这个错误。

最佳答案

MessageBox 将在显示时阻塞您的 UI 线程,并且您在单击对话框之前不会到达 timer.Stop()。 尝试在显示 MessageBox 之前停止计时器:

private void timer2_Tick(object sender, EventArgs e)
{
    DateTime Date = DateTimePickerReminderDate.Value;
    DateTime Time = DateTimePickerReminderTime.Value;
    if (DateTime.Now.CompareTo(Date) > 0 && DateTime.Now.CompareTo(Time) > 0) 
    {
        timer2.Stop();     

        Date = DateTime.MaxValue;
        Time = DateTime.MaxValue; 
        MessageBox.Show("Your Reminder");
    }
}  

关于c# - C# WinForm 中 MessageBox 的连续弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391226/

相关文章:

c# - 防止表单关闭卡住调用调用

c# - 检查碰撞的更简单方法?

javascript - 禁用滚动条后显示 JQuery 对话框

c# - 如何在 MessageBoxIcon.Stop image 等形式上显示图像

flutter - 滑动 flutter 中留下的容器(如电报之类的消息框)

c# - 调用泛型方法并在运行时设置泛型类型

c# - 无法在 Monotouch 中正确使用 EventHandler 的通用版本

c# - 为什么路径正在改变

c# - .net 通用类型对象转换为与其所持有的类型不同的类型

c# - NHibernate POCO/基础知识