Delay function

A

Alex

Hi, everyone.

I'm C++ programmer, but I'm starting to write some code in C#. I need
in C# some function, which delays code execution. In C++ I have a
cute function:
void CSomeClass::Delay( int nSec )
{
time_t t0 = time(0);
while (difftime(time(0), t0) < nSec )
{

}
}

I've accomplished the same in C# the following way:
protected void DelayExecution(int nSeconds)
{
System.DateTime tmCurrent;
System.DateTime tmStart = System.DateTime.Now;

System.TimeSpan tmspStart = new TimeSpan( tmStart.Hour,
tmStart.Minute, tmStart.Second);

double dStartSeconds = tmspStart.TotalSeconds;
double dCurrentSeconds = dStartSeconds;

while (dCurrentSeconds - dStartSeconds < nSeconds )
{
tmCurrent = System.DateTime.Now;
System.TimeSpan tmspCurrent = new TimeSpan(tmCurrent.Hour,
tmCurrent.Minute, tmCurrent.Second);

dCurrentSeconds = tmspCurrent.TotalSeconds;
}
}

But my code written in C# looks much more cumbersome comparing to the C
++ function. But probably I don't know yet all the possibilites, that
C# offers and the Delay function can be done in C# more elegantly?

Thanks,
Alex
 
M

mpetrotta

Hi, everyone.

I'm C++ programmer, but I'm starting to write some code in C#. I need
in C# some function, which delays code execution. In C++ I have a
cute function:
void CSomeClass::Delay( int nSec )
{
time_t t0 = time(0);
while (difftime(time(0), t0) < nSec )
{

}

}

I've accomplished the same in C# the following way:
protected void DelayExecution(int nSeconds)
{
System.DateTime tmCurrent;
System.DateTime tmStart = System.DateTime.Now;

System.TimeSpan tmspStart = new TimeSpan( tmStart.Hour,
tmStart.Minute, tmStart.Second);

double dStartSeconds = tmspStart.TotalSeconds;
double dCurrentSeconds = dStartSeconds;

while (dCurrentSeconds - dStartSeconds < nSeconds )
{
tmCurrent = System.DateTime.Now;
System.TimeSpan tmspCurrent = new TimeSpan(tmCurrent.Hour,
tmCurrent.Minute, tmCurrent.Second);

dCurrentSeconds = tmspCurrent.TotalSeconds;
}

}

But my code written in C# looks much more cumbersome comparing to the C
++ function. But probably I don't know yet all the possibilites, that
C# offers and the Delay function can be done in C# more elegantly?

Your C# code is fairly circuitous, so I'm basing this on your C++
code. Here's a literal translation:

static void Delay(int seconds)
{
TimeSpan diff = new TimeSpan(0, 0, seconds);
DateTime end = DateTime.Now.Add(diff);
while (DateTime.Now < end)
{
;
}
}

Don't do this, though. It'll pin your CPU for the delay period, for
no reason. If you need to wait (and you can't just block on a mutex,
or wait for an event), then use Thread's Sleep method:

System.Threading.Thread.Sleep(5000); // 5 seconds

Michael
 
J

Jon Skeet [C# MVP]

I'm C++ programmer, but I'm starting to write some code in C#. I need
in C# some function, which delays code execution. In C++ I have a
cute function:
void CSomeClass::Delay( int nSec )
{
time_t t0 = time(0);
while (difftime(time(0), t0) < nSec )
{

}
}

I wouldn't call that cute - I'd call it a processor hog.

Even in C/C++ I'd use a sleep function. In C#, use Thread.Sleep. That
will, of course, only put the current thread to sleep, not the whole
application.
But my code written in C# looks much more cumbersome comparing to the C
++ function. But probably I don't know yet all the possibilites, that
C# offers and the Delay function can be done in C# more elegantly?

If you really want your C++ code rewritten in C#, it could indeed be
done more elegantly:

protected void DelayExecution(int nSeconds)
{
DateTime end = DateTime.UtcNow.AddSeconds (nSeconds);

while (DateTime.UtcNow < end)
{
}
}

I'd still recommend Thread.Sleep though :)

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

Alex,

I am curious, what you are doing is the same thing as a spin wait. Why
not use the static SpinWait method on the Thread class (it only offers
iterations), or why not use the Sleep method? Sleep is different, in that
you give up your time slice on the processor, and you might be looking for a
spin wait specifically.

I am curious, are you using this for some sort of locking situation? If
so, what is it about the synchronization methods in .NET that is lacking?
 
W

Willy Denoyette [MVP]

Alex said:
Hi, everyone.

I'm C++ programmer, but I'm starting to write some code in C#. I need
in C# some function, which delays code execution. In C++ I have a
cute function:
void CSomeClass::Delay( int nSec )
{
time_t t0 = time(0);
while (difftime(time(0), t0) < nSec )
{

}
}

I've accomplished the same in C# the following way:
protected void DelayExecution(int nSeconds)
{
System.DateTime tmCurrent;
System.DateTime tmStart = System.DateTime.Now;

System.TimeSpan tmspStart = new TimeSpan( tmStart.Hour,
tmStart.Minute, tmStart.Second);

double dStartSeconds = tmspStart.TotalSeconds;
double dCurrentSeconds = dStartSeconds;

while (dCurrentSeconds - dStartSeconds < nSeconds )
{
tmCurrent = System.DateTime.Now;
System.TimeSpan tmspCurrent = new TimeSpan(tmCurrent.Hour,
tmCurrent.Minute, tmCurrent.Second);

dCurrentSeconds = tmspCurrent.TotalSeconds;
}
}

But my code written in C# looks much more cumbersome comparing to the C
++ function. But probably I don't know yet all the possibilites, that
C# offers and the Delay function can be done in C# more elegantly?

Thanks,
Alex


Why not simply use System.Threading.Thread.Sleep(), instead of burning
cycles while waiting.

Thread.Sleep(1000); // sleep 1 second.
....

Willy.
 
A

Alex

Thanks everybody for the answers.

Indeed, because thread using in C# is much easier than in C++ some
things can be done differently. Though I haven't decided yet which
advise to take because I don't have to worry about the rest of the
application, while current function is "sleeping". The only thing
this very small application does - it's starting in background another
application, which in turn is connecting to different instances of SQL
Server. Because login process to SQL Server takes some time and some
other lengthy checking and initializations must be done, my
application has to "sleep" in between consecutive logins.

Thanks again,
Alex
 
M

mpetrotta

Thanks everybody for the answers.

Indeed, because thread using in C# is much easier than in C++ some
things can be done differently. Though I haven't decided yet which
advise to take because I don't have to worry about the rest of the
application, while current function is "sleeping". The only thing
this very small application does - it's starting in background another
application, which in turn is connecting to different instances of SQL
Server. Because login process to SQL Server takes some time and some
other lengthy checking and initializations must be done, my
application has to "sleep" in between consecutive logins.

The other application will do its thing faster if the first app isn't
hogging the CPU...
 
J

Jon Skeet [C# MVP]

Alex said:
Thanks everybody for the answers.

Indeed, because thread using in C# is much easier than in C++ some
things can be done differently.

You make it sound like it's hard to make the current thread sleep in
C++. It's really not - there's the sleep() function which has been in
the C standard library (and thus C++) for as long as I've been coding.
 
L

Larry Smith

Just an FYI that the "Sleep()" method is one of the most abused and
insidious functions around. Don't use it for synchronization purposes unless
you absolutely have to (which is almost never). It can cause everything from
bottlenecks to race conditions depending on how you use it. Use an
appropriate synchronization class when possible.
 
Top