I wrote some code to create a reminder. It should warn me through a notification when it's 10 am. To make this possible I created a DateTime object as DateTime.Now to get local time, and I used a timer to check every minute if it was the same time I wanted to be warned. The problem is that my application notify me only when form loads, but it doesn't when the time comes and the app is already running. I'll leave you the code below. Thanks in advance.
public partial class Form1 : Form
{
NotifyIcon notify;
DateTime now;
public Form1()
{
InitializeComponent();
notify = new NotifyIcon()
{
Visible = true,
Icon = Properties.Resources.icon,
BalloonTipTitle = this.Text
};
now = DateTime.Now;
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void notification()
{
while(true)
{
if (now.Hour.Equals(10) && now.Minute.Equals(30))
{
notify.BalloonTipText = "It's 10:30 am";
notify.ShowBalloonTip(3000);
timer1.Stop();
notify.Dispose();
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
notification();
}
}
Remove while(true)
loop from notification
. You have tick timer which will call notification
regularly - and now you check the time infinitely after first tick.
now
must be assigned on every tick, not at startup.
:
private void notification()
{
DateTime now = DateTime.Now;
if (now.Hour == 10 && now.Minute == 30)
{
notify.BalloonTipText = "It's 10:30 am";
notify.ShowBalloonTip(3000);
timer1.Stop();
}
}