Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am needing help with the dispatcher class from WPF and would like it to function much like the Windows Forms Timer class example below. To see the Windows Form Application it would require you to copy the code into a new WForms app and place the code in a Form1.cs file. In the Form 1 designer place a link that says remind me later. You would need an additional form name it reminder add a DateTimePicker a button next to it and add a calendar. This is a correlation! I am needing help doing the same thing with WPF. The timer class plays a similar role as the Dispatcher class. How do I create the proper interval with the Dispatcher.Interval property, if that is even where it needs to go?


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Reminder dlg = new Reminder();
        private DateTime myRemindTime;

        private void LblReminder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                myRemindTime = dlg.MyMonthCalendar.SelectionStart.AddHours(dlg.MyTimePicker.Value.Hour).AddMinutes(dlg.MyTimePicker.Value.Minute).AddSeconds(dlg.MyTimePicker.Value.Second);
                this.Visible = false;//part where you would change to make it visible to user
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (myRemindTime.CompareTo(DateTime.Now) < 0)
            {
                this.Visible = true;
            }
        }
    }
Posted
Updated 1-Apr-13 8:52am
v3
Comments
Sergey Alexandrovich Kryukov 2-Apr-13 10:48am    
The inquirer's account was deactivated due to extreme rudeness, as a result of abuse reports.
I removed all comments as trash talking.
—SA

1 solution

You should never try to use System.Windows.Forms.Timer with WPF, and almost never even with Forms. The accuracy of this timer is prohibitively bad for most applications. There are (at least) three more timer types you can use:

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx[^].

Another option is using a separate thread with loop and System.Threading.Thread.Sleep, which I think is the easiest and most straightforward.

Now, as a timer event is not guaranteed to invoke in your UI thread, you generally need to invoke any method using the UI methods/properties on the UI thread, using the Dispatcher. You don't need (and cannot) do it with System.Windows.Threading.DispatcherTimer, but if you use a separate thread or other timer types, you need to use the dispatcher.

Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Apr-13 10:47am    
The inquirer's account was deactivated due to extreme rudeness, as a result of abuse reports.
I removed all comments as trash talking.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900