Search code examples
c#wpfdatetimenotificationstoast

Show Windows toast every day in a specific time


I have a wpf application where I would like to create some notifications that I want to show as a windows toast popup. I have a code that can show this popup message if I assign it to a button. Could you please advise me a solution for showing this popup automatically in a given hour?

        TimeSpan start = new TimeSpan(17, 05, 59);
        TimeSpan end = new TimeSpan(17, 06, 01);
        TimeSpan now = DateTime.Now.TimeOfDay;

        if ((now > start) & (now < end))
        {
            var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var text = xml.GetElementsByTagName("text");
            var toast = new ToastNotification(xml);
            ToastNotificationManager.CreateToastNotifier("Here is an alert that I want to show exactly at 5:06 PM").Show(toast);
        }

Solution

  • You can use System.Threading.Timer to achieve your Task. TimerWithCallback.

    Constructor: Timer(TimerCallback, Object, TimeSpan, TimeSpan)