0

Background:

I need a high resolution timer for an embedded system solution, so I decide to use MicroTimer from The Code Project...

BTW, I've developed a Windows Forms application to test its efficiency in such applications and for avoiding "cross-thread operation...", I have to use invocation methods, BackgroundWorker, etc. and decide to use this code:

private void btnMicorTimer_Click(object sender, EventArgs e)
{
    // Instantiate new MicroTimer
    MicroLibrary.MicroTimer microTimer = new MicroLibrary.MicroTimer();
    // Add event handler
    microTimer.MicroTimerElapsed +=
        new MicroLibrary.MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent);

    // Call micro timer every 1000µs (1ms)
    microTimer.Interval = 1000;

    // Can choose to ignore event if late by Xµs (by default will try to catch up)
    // microTimer.IgnoreEventIfLateBy = 500;

    microTimer.Enabled = true; // Start timer

    // Do something whilst events are happening. For demo sleep, 2000 ms (2 sec).
    System.Threading.Thread.Sleep(2000);

    microTimer.Enabled = false; // Stop timer
}

private void OnTimedEvent(object sender,
                          MicroLibrary.MicroTimerEventArgs timerEventArgs)
{
    string response = string.Format(
                    "Count = {0:#,0}  Timer = {1:#,0} µs | " +
                    "LateBy = {2:#,0} µs | ExecutionTime = {3:#,0} µs",
                    timerEventArgs.TimerCount, timerEventArgs.ElapsedMicroseconds,
                    timerEventArgs.TimerLateBy, timerEventArgs.CallbackFunctionExecutionTime);

    // Do something small that takes significantly less time than Interval
    if (listBox1.InvokeRequired)
    {
        Invoke(new MethodInvoker(
            delegate
            {
                listBox1.Items.Add(response);
            }));
    }
    else
        listBox1.Items.Add(response);
}

Problem:

As said before, the application is halting immediately after a button is pressed! Briefly this is what happens after btnMicorTimer_Click :( It's look like the project is trapped in an infinite loop.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What do you mean by halting? You did add a Sleep on the UI thread... – Emond Jul 04 '12 at 06:22
  • I've desired this: http://postimage.org/image/f0lnt11bp/ But site rules doesn't let me add this photo in my post! BTW: Application give huge resources and not responding ... – Saeed Farid Jul 04 '12 at 06:36

1 Answers1

0

The peak you see can easily be explained:

You are requesting a UI update every millisecond by adding an item to the listbox.

That will slow down the response of the UI considerably.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Tanx Erno, but what was caused wonder to me wasn't PEAK in UI (as I foresight for it): What's unacceptable to me is: Why must application not responding! delay doesn't matter. I forced each time (even with 100ms of MicroTimer working) to End task application to back to normal... – Saeed Farid Jul 04 '12 at 08:03
  • It is not responding because you are spamming the UI thread with messages. It is simply too busy to respond to user actions. (I don't understand the last line of your comment) – Emond Jul 04 '12 at 08:16
  • Thanks for your time, I mean: when my spamming the UI thread finished, basically it must respond normally! Isn't it true? – Saeed Farid Jul 04 '12 at 08:42
  • No, it will respond normally AFTER all the messages have been processed. In a situation like this it might be better to add the messages to a list and update the UI with this list every second or so. That will free the UI thread. Who can read 1000 messages in one second anyway? – Emond Jul 04 '12 at 08:46
  • OK, I try this before and it's work with List: but my point is about this case (without generic list, ...) => even by changing System.Threading.Thread.Sleep(100); {for 100 messages!} application would not respond after 1 hour! – Saeed Farid Jul 04 '12 at 08:57