TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Delay function

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 26-09-2007, 07:48 AM   #1
Alex
Guest
 
Posts: n/a
Delay function

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

 
Unread 26-09-2007, 07:49 AM   #2
Alex
Guest
 
Posts: n/a
Re: Delay function

On Sep 25, 12:11 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.be> wrote:
> "Alex" <alsim...*************> wrote in message
>
> news:1190734975.446885.318790@50g2000hsm.googlegro ups.com...
>
>
>
>
>
> > 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 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



> > Thanks,
> > Alex

>
> Why not simply use System.Threading.Thread.Sleep(), instead of burning
> cycles while waiting.
>
> Thread.Sleep(1000); // sleep 1 second.
> ...
>
> Willy.- Hide quoted text -
>
> - Show quoted text -



 
Unread 26-09-2007, 07:49 AM   #3
Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
Re: Delay function

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?


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Alex" <alsim123*************> wrote in message
news:1190734975.446885.318790@50g2000hsm.googlegro ups.com...
> 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
>



 
Unread 26-09-2007, 07:49 AM   #4
mpetrotta@gmail.com
Guest
 
Posts: n/a
Re: Delay function

On Sep 25, 7:42 am, Alex <alsim...*************> wrote:
> 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?


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

 
Unread 26-09-2007, 07:49 AM   #5
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Delay function

On Sep 25, 4:42 pm, Alex <alsim...*************> wrote:
> 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 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

 
Unread 26-09-2007, 07:49 AM   #6
mpetrotta@gmail.com
Guest
 
Posts: n/a
Re: Delay function

On Sep 25, 10:26 am, Alex <alsim...*************> wrote:
> On Sep 25, 12:11 pm, "Willy Denoyette [MVP]"
>
> <willy.denoye...@telenet.be> wrote:
> > "Alex" <alsim...*************> wrote in message

>
> >news:1190734975.446885.318790@50g2000hsm.googlegr oups.com...

>
> > > 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 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...

 
Unread 26-09-2007, 07:50 AM   #7
Willy Denoyette [MVP]
Guest
 
Posts: n/a
Re: Delay function

"Alex" <alsim123*************> wrote in message
news:1190734975.446885.318790@50g2000hsm.googlegro ups.com...
> 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
>



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

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

Willy.

 
Unread 26-09-2007, 07:50 AM   #8
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Delay function

Alex <alsim123*************> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Unread 26-09-2007, 07:52 AM   #9
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Delay function

Alex <alsim123*************> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Unread 26-09-2007, 07:54 AM   #10
Alex
Guest
 
Posts: n/a
Re: Delay function

On Sep 25, 12:11 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.be> wrote:
> "Alex" <alsim...*************> wrote in message
>
> news:1190734975.446885.318790@50g2000hsm.googlegro ups.com...
>
>
>
>
>
> > 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 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



> > Thanks,
> > Alex

>
> Why not simply use System.Threading.Thread.Sleep(), instead of burning
> cycles while waiting.
>
> Thread.Sleep(1000); // sleep 1 second.
> ...
>
> Willy.- Hide quoted text -
>
> - Show quoted text -



 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delay function Alex C#(C Sharp) 6 26-09-2007 07:48 AM
Delay function Alex C#(C Sharp) 4 26-09-2007 07:44 AM
Delay with IE7 Start up jaybeast69 Internet Explorer 3 28-08-2007 05:17 PM
Delay with IE7 Start up jaybeast69 Internet Explorer 1 28-08-2007 05:12 PM
Keyboard delay Scott Internet Explorer 9 28-08-2007 03:15 PM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 09:04 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional