Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous Popup of MessageBox in C# WinForm

This is my Code:

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

When I set the reminder it working as expected and Showing the message at right sated time.But the problem is,it continuously giving pop up of messagebox I tried to clear this bug but I am unsuccessful in it.So right now I need the experts advice so please help me for clear this bug.

like image 278
Rock Avatar asked Dec 15 '22 08:12

Rock


1 Answers

The MessageBox will block your UI thread while displayed and you will not reach the timer.Stop() before you click the dialog away. Try stopping the timer before you display your 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");
    }
}  
like image 183
Franz Wimmer Avatar answered Dec 31 '22 13:12

Franz Wimmer