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:
elay( 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
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:
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
{
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