Rock
Rock

Reputation: 73

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.

Upvotes: 4

Views: 479

Answers (1)

Franz Wimmer
Franz Wimmer

Reputation: 1487

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

Upvotes: 12

Related Questions