Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Using a delay routine in C#

Status
Not open for further replies.

maniac84

Full Member level 6
Joined
Mar 4, 2012
Messages
337
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
3,661
Hi guys,
Just wondering is there any other delay routine other than
Code:
Thread.Sleep();
in C#. I found out that the timing of
Code:
Thread.Sleep(700);
is different on every pc. Is there a delay routine which is more accurate and the same if use on any pc?
 

clock_gettime() if you have a POSIX system. Not sure C# has that, but most OSs support it, and would allow you to check the current time. i.e. sleep and then check. Don't expect to be able to accurately delay in milliseconds like in your example, since you can't guarantee that other processes won't pre-empt. Generally, avoid using delays in your apps.
 

You could write a delay routine using the DateTime functions.

Code:
/*--- Timer ---*/

  private DateTime start_time = new DateTime();
  private DateTime now_time = new DateTime();
  private TimeSpan time_interval = new TimeSpan();

/*--- Code ---*/

  start_time = DateTime.Now;
  now_time = DateTime.Now;
  time_interval = nowtime - starttime;
 

You could make a timer,say Mytimer1,put a variable name it mycounter have it increased by one till 1000
enable this timer,then start the timer then enter an infinit loop
Code:
for(;;)
{
       if (mycounter == 999) 
       {
        //do what you want to do
        break;
        }
}
the program will be trapped inside infinit loop till mycounter become 999,this make a delay time period call it x
you could measure this x varible in time units using the above formula written by btbass
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top