Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

c# - DispatcherTimer doesn't work in Console

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...