Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 1-Apr-13 7:16am
Edited 1-Apr-13 9:52am
v3
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100