WPF Dispatcher = Windows窗体Timer类 [英] WPF Dispatcher = Windows Forms Timer class

查看:123
本文介绍了WPF Dispatcher = Windows窗体Timer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要WPF的调度程序类的帮助,并希望它的功能类似于下面的Windows窗体计时器类示例。要查看Windows窗体应用程序,需要将代码复制到新的WForms应用程序中,并将代码放在Form1.cs文件中。在表单1设计器中放置一个链接,告诉我稍后。您需要一个额外的表单名称提醒,在其旁边添加一个DateTimePicker按钮并添加日历。这是一个相关性!我需要帮助用WPF做同样的事情。计时器类与Dispatcher类的作用类似。如何使用Dispatcher.Interval属性创建适当的间隔,如果它甚至需要去的地方?





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

推荐答案

你永远不应该尝试使用使用WPF的System.Windows.Forms.Timer ,几乎从不使用Forms。对于大多数应用来说,这个定时器的准确性非常糟糕。你可以使用(至少)三种计时器类型:



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 [ ^ ]。



另一种选择是使用带循环的单独线程和 System.Threading.Thread.Sleep ,我认为这是最简单和最直接的。



现在,由于无法保证在UI线程中调用计时器事件,因此通常需要使用Dispatcher在UI线程上使用UI方法/属性调用任何方法。您不需要(也不能)使用 System.Windows.Threading.DispatcherTimer 来完成它,但如果您使用单独的线程或其他计时器类型,则需要使用调度员。



请查看我过去的答案:

Control.Invoke()vs。Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



-SA
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


这篇关于WPF Dispatcher = Windows窗体Timer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆