Note that there are some explanatory texts on larger screens.

plurals
  1. PODispatcherTimer doesn't work in Console
    primarykey
    data
    text
    <p>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. </p> <p>Can you use dispatcher timer with UnitTest or in Console mode? DailyAlarm works when I run it in a form.</p> <p>Here's my code to call the timer</p> <pre><code>[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; } } </code></pre> <p>Here's my timer code </p> <pre><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 &lt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload