C# WinForm 中 MessageBox 的连续弹出

Continuous Popup of MessageBox in C# WinForm

这是我的代码:

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();     
    }
}   

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

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");
    }
}