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?
 

sky_123

Advanced Member level 4
Joined
Feb 26, 2011
Messages
1,170
Helped
279
Reputation
566
Reaction score
273
Trophy points
1,363
Activity points
9,602
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.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
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;
 

EgrK

Junior Member level 2
Joined
Jul 31, 2012
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
East part of world
Activity points
1,438
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top