DispatcherTimer doesn't work in Console

故事扮演 提交于 2019-12-19 03:36:48

问题


I'm curious as to why dispatcher timer doesn't work in console mode. I created a simple alarm that does something when the timer reaches it's limit.

Can you use dispatcher timer with UnitTest or in Console mode? DailyAlarm works when I run it in a form.

Here's my code to call the timer

[TestClass]
public class UnitTest1
{
    bool runTest = true;
    [TestMethod]
    public void TestDailyAlarm()
    {
        DateTime alarmTime = new DateTime();
        alarmTime= DateTime.Now;
        alarmTime = alarmTime.AddSeconds(5);

        // MessageBox.Show(alarmTime.ToString());

        DailyAlarm alarm = new DailyAlarm(alarmTime);
        alarm.DailyAlarmEvent += alarm_DailyAlarmEvent;
        alarm.Start();
        while (runTest)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }

    void alarm_DailyAlarmEvent(EventArgs e)
    {            
        MessageBox.Show("Alarm On");
        runTest = false;
    }
}

Here's my timer code

public class DailyAlarm
{
    #region Timer
    DispatcherTimer timer;
    #endregion

    #region AlarmTime
    DateTime _alarmTime;
    #endregion

    #region Event
    public delegate void DailyAlarmHandler(EventArgs e);
    public event DailyAlarmHandler DailyAlarmEvent;
    #endregion
    public DailyAlarm(System.DateTime alarmTime)
    {
        if (alarmTime < DateTime.Now)
        {
            alarmTime = alarmTime.AddDays(1);
        }
         _alarmTime = alarmTime;
         TimeSpan timeRemaining = alarmTime.Subtract(DateTime.Now);
        timer = new DispatcherTimer();
        timer.Tick += AlarmEvent;
        timer.Interval = timeRemaining;

    }

    public void Start()
    {
        timer.Start();
    }

    private void AlarmEvent(object sender, EventArgs e)
    {
        DailyAlarmEvent(null); 
        // Calculate next Alarm 
        _alarmTime = _alarmTime.AddDays(1);
        TimeSpan timeRemaining = _alarmTime.Subtract(DateTime.Now);
        Utilities.DispatcherTimer_ChangeInterval(ref timer, timeRemaining);           
    }

    public void Stop()
    {
        if (timer != null)
            timer.Stop();
    }
}

回答1:


The console and unit test environment by default don't have a dispatcher to run your dispatcher timer.

You can still use Dispatcher.CurrentDispatcher to create a Dispatcher to run your code.

There's an example of its usage at http://consultingblogs.emc.com/crispinparker/archive/2010/10/22/unit-testing-a-wpf-dispatchertimer-method.aspx

With this DispatcherHelper you can test your code with:

    [TestMethod]
    public void TestMethod1()
    {
        Action test = () =>
            {
                var dailyAlarm = new DailyAlarm(DateTime.Now.AddSeconds(5.0));
                dailyAlarm.DailyAlarmEvent += dailyAlarm_DailyAlarmEvent;
                dailyAlarm.Start();
            };

        DispatcherHelper.ExecuteOnDispatcherThread(test, 20);
    }

    void dailyAlarm_DailyAlarmEvent(EventArgs e)
    {
        // event invoked when DispatcherTimer expires
    }



回答2:


DispatcherTimer fires its Tick event on the UI thread. And you are running your code in a console mode. That's is the answer, I think!



来源:https://stackoverflow.com/questions/19351473/dispatchertimer-doesnt-work-in-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!