Similar to the timer class except for this timer does not loose time over time. This timer has almost no drift. Source: John Leidegren (2010, http://stackoverflow.com/a/3250747/2352507)
Пример #1
0
        private void Capture_Load(object sender, EventArgs e)
        {
            /////////// Setup logger ///////////
            logger = NLog.LogManager.GetCurrentClassLogger();
            logger.Info("Starting application.");

            //target.Name = "display";
            //target.Layout = "${time} | ${Threadid} | ${level} | ${logger} | ${message}";
            //target.ControlName = "richTextBox1";
            //target.FormName = "Recorder";
            //target.UseDefaultRowColoringRules = true;
            //NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, NLog.LogLevel.Trace);
            //target.MaxLines = 1024;
            //target.AutoScroll = true;


            ///////////// Set new state to prevent the system from entering sleep mode /////////////
            // Source: David Anson @ Microsoft (2009) http://dlaa.me/blog/post/9901642
            m_previousExecutionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
            if (0 == m_previousExecutionState)
            {
                MessageBox.Show("Failed to set system state for sleep mode.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // No way to recover; fail gracefully
                Close();
            }


            /////////// Set timer to first snapshot /////////// 
            //(Note: this must by setup on the UI thread)
            try
            {
                logger.Info("Configuring Timer");
                timer = new FixedStepDispatcherTimer(new TimeSpan(0, 0, 1));
                DateTime now = DateTime.Now;
                DateTime firstTick = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second + 1);
                timer.Restart(firstTick);
            }
            catch (Exception ex)
            {
                logger.Error("Error creating DispatcherTimer: " + ex.Message);
            }

            logger.Info("Configuring Recorder BackgroundWorker");
            bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            bw.RunWorkerAsync();
        }
Пример #2
0
        private void Capture_Load(object sender, EventArgs e)
        {
            /////////// Setup logger ///////////
            logger = NLog.LogManager.GetCurrentClassLogger();
            logger.Info("Starting application.");

            //target.Name = "display";
            //target.Layout = "${time} | ${Threadid} | ${level} | ${logger} | ${message}";
            //target.ControlName = "richTextBox1";
            //target.FormName = "Recorder";
            //target.UseDefaultRowColoringRules = true;
            //NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, NLog.LogLevel.Trace);
            //target.MaxLines = 1024;
            //target.AutoScroll = true;


            ///////////// Set new state to prevent the system from entering sleep mode /////////////
            // Source: David Anson @ Microsoft (2009) http://dlaa.me/blog/post/9901642
            m_previousExecutionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
            if (0 == m_previousExecutionState)
            {
                MessageBox.Show("Failed to set system state for sleep mode.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // No way to recover; fail gracefully
                Close();
            }


            /////////// Set timer to first snapshot ///////////
            //(Note: this must by setup on the UI thread)
            try
            {
                logger.Info("Configuring Timer");
                timer = new FixedStepDispatcherTimer(new TimeSpan(0, 0, 1));
                DateTime now       = DateTime.Now;
                DateTime firstTick = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second + 1);
                timer.Restart(firstTick);
            }
            catch (Exception ex)
            {
                logger.Error("Error creating DispatcherTimer: " + ex.Message);
            }

            logger.Info("Configuring Recorder BackgroundWorker");
            bw         = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            bw.RunWorkerAsync();
        }