0

I am porting part of firmware to a gui written in c#. I found out that

nop()

is a function that actually does nothing and delays the execution by some number of clock cycles.

Lets say if I use nop() it will delay the execution in firmware by two milliseconds. How do I delay things in c#?

Thanks in advance,

Vikyboss
  • 940
  • 2
  • 11
  • 23
  • A .NET GUI application is not the same as a firmware. If there is a reason for a nop() in the firmware, there is not necessarily a reason for it in a .NET GUI application. Check if the nop() calls are required and, if not, just skip them. – dtb Jul 05 '11 at 15:59
  • Thanks everyone. Ended up using System.Threading.Thread.Sleep(milliseconds); as there are other delays associated with the gui that need to be loaded. Using pretty big delay(milliseconds compared to microseconds) will suit the need of gui development. – Vikyboss Jul 05 '11 at 16:26
  • If you are doing GUI programming, you probably shouldn't be doing any sort of delays. Instead make use of async programming patterns to fire off work, and be notified of their status, instead of sleeping/blocking. – Alan Jul 05 '11 at 18:37

4 Answers4

4

System.Threading.Thread.Sleep(milliseconds);

Note that blocking your GUI's main thread for more than a few milliseconds is generally a bad idea. It may be better to use a timer that wakes up after a few milliseconds, to allow event-handling to happen in the meantime. Also, if you want to pause before doing some I/O, consider using asynchronous I/O instead.

Also note that neither Windows or .NET provide any real-time guarantees. A call to Thread.Sleep(2) will pause for at least 2 milliseconds, but that thread may not resume execution for many milliseconds beyond that.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
2
Thread.Sleep(1000);

Will sleep the current execution for 1 second.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
0

Thread.Sleep.

For example: Thread.Sleep(2);

Be aware that windows has a minimum resolution of around 15ms however, so you might not get exatly 2ms. For more info, see What is the impact of Thread.Sleep(1) in C#?

Community
  • 1
  • 1
George Duckett
  • 31,770
  • 9
  • 95
  • 162
0

Gonna go against the grain and say, if you need strict timing, Thread.Sleep isn't a good idea.

I recommend using Manual/Auto ResetEvents with a System.Timers timer instead.

Alan
  • 45,915
  • 17
  • 113
  • 134