-1

I have got the next problem. In windows forms I need to make a timer that simulates a while loop. I have runned some tests and a while loop is about 27 times faster than a timer with 1 interval. So my question is, how can I simulate a while loop with my timer. This is the code I used to test the interval:

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
             m_nI++;
             lbltimer->Text = m_nI++.ToString();
             if (m_nI > 10000000)
                 {
                     timer1->Enabled = false;
                 }

             while(1)
             {
                 m_nIMin++;
                 lblLoop->Text = m_nIMin.ToString();
                 Application::DoEvents();
                 if (m_nIMin > 10000000)
                 {
                     break;
                 }
             }
         }

m_nI is declared global just as m_nIMin they are both ints. Any respond in c++/cli or c++ would be welcome. Thanks in advance.

PS. I know Application::DoEvents() isn't good to use but its the only solution on testing a infinite while loop in windowsforms.

Thealon
  • 1,935
  • 1
  • 13
  • 22
  • What are you trying to *accomplish* with this code? – Cody Gray - on strike May 23 '14 at 08:09
  • It is very important I accomplish the speed for a project i'm currently working on. – Thealon May 23 '14 at 08:14
  • This whole question doesn't make any sense. If you want a while loop, then use a while loop. Certainly don't use it in the Timer_Tick event handler, though. You can't run a blocking loop on the UI thread without hogging the UI. That's why you need DoEvents. Solve the problem by using a background thread. – Cody Gray - on strike May 23 '14 at 08:23
  • Could you give me a sample code of the background thread? Ill take it as an answer. – Thealon May 23 '14 at 08:28
  • 1
    The UI thread only needs to do things that make sense to a human. Who's eyes are utterly incapable of seeing a number change at 1 millisecond increments. It is a complete blur, it starts to get blurry at 50 msec updates. Plenty slow enough for a Timer, a hot while() loop is complete overkill. Avoid writing senseless code and problems like this just disappear. – Hans Passant May 23 '14 at 08:42
  • @HansPassant I know thats why i'm looking for an alternative to this while loop. I'm updating old C console code to newer c++/cli code. The console code was using a while loop that was infinite and now I need the same speed in winforms. Although a timer isn't fast enough a backgroundworker may be the solution. But any help is appreciated. – Thealon May 23 '14 at 08:45

1 Answers1

1

The timer resolution is given by the system heartbeat. This typically defaults to 64 beats/s which is 15.625 ms. However there are ways to modify these system wide settings to achieve timer resolutions down to 1 ms or so.

For more details read Why .NET Timers are limited to 15ms

on the other hand while loop runs continuously and may cause UI to freeze if not used properly.

Community
  • 1
  • 1
Manish Dalal
  • 1,768
  • 1
  • 10
  • 14
  • But how I achieve the same speed as in a while loop, that was my question – Thealon May 23 '14 at 09:35
  • through timer only, YOU CANNOT.... but you can find a way-out (called jugaad) by knowing how much time one iteration of the loop takes, and run the loop that many times within one timer event ....... Alternatively, run a while loop with Application.DoEvents (But read http://blog.codinghorror.com/is-doevents-evil/ before using DoEvents) – Manish Dalal May 23 '14 at 09:53