By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

How to introduce delay in C#..

P: n/a
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in loop
to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay inbetween
slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak


Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi,

To make some delay in c#,use the for Loops,

for(i=0;i<=delay;i++);

Adjust the delay value according to ur delay time.Here delay is a long int.

"Maheshkumar.R" wrote:
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in loop
to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay inbetween
slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak


Nov 17 '05 #2

P: n/a
No aravind. I know this shortcut. But I'm looking something in Keyword
perspective or any inbuilt function from C#..

Mahesh
"Aravind" <Ar*****@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
Hi,

To make some delay in c#,use the for Loops,

for(i=0;i<=delay;i++);

Adjust the delay value according to ur delay time.Here delay is a long int.
"Maheshkumar.R" wrote:
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in loop to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay inbetween slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak


Nov 17 '05 #3

P: n/a
Aravind <Ar*****@discussions.microsoft.com> wrote:
To make some delay in c#,use the for Loops,

for(i=0;i<=delay;i++);

Adjust the delay value according to ur delay time.Here delay is a long int.


No, that's a terrible way of delaying! It puts the processor into a
tight loop!

If the OP wants to do something on a regular basis in a UI, then a
Timer is the best bet:
http://www.pobox.com/~skeet/csharp/threads/timers.shtml
To delay a single thread, use Thread.Sleep - but don't do this in the
UI thread, or it will make the UI unresponsive.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
Hi, the easiest and best way would be to use a timer. Timers are objects
that can be set up to tell you when a specified period of time has elapsed.
It's certainly a better solution than just looping or doing a Thread.Sleep.

Using a TimerCallBack delegate, you specify which method you want the timer
to call. Like so...

using System.Threading;

class
{
Timer tmr;

public void ShowSlide( object state )
{
// Do some stuff to show a slide
}

public void StartSlideshow()
{
TimerCallBack tcb = new TimerCallback( ShowSlide );
timer = new Timer( tcb, null, 1000, 1000 );
}
}

Obviously you can change this to be able to specify what time period you
want to leave between slides etc. Any problems post back.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:Oe*************@TK2MSFTNGP15.phx.gbl...
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in loop to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay inbetween slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak

Nov 17 '05 #5

P: n/a
So, its confirmed that we dont have any straight C# keyword or methods for
delay...?

I thought of going for Threading/timer initially but I wants to try
something else than this.

Here is my exact requirement :
As you all know fastforward in movie right..? 2x , 4x, 6x , 8x etc in VCD
player.
I have achieved fastforward by adding 'n+' slices..but when i comes to
Delaying i mean -2x , -4x, -6x etc...

I dont know how to solve this delaying...I mean i dont want to push this
function into threading or timer event or loop delay....let see if any one
proceeds this DELAY without delayinnnnnnnn......

Thanks for all your inputs..

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak

"Tim Haughton" <ti*********@gmail.com> wrote in message
news:k3*******************@fe08.news.easynews.com. ..
Hi, the easiest and best way would be to use a timer. Timers are objects
that can be set up to tell you when a specified period of time has elapsed. It's certainly a better solution than just looping or doing a Thread.Sleep.
Using a TimerCallBack delegate, you specify which method you want the timer to call. Like so...

using System.Threading;

class
{
Timer tmr;

public void ShowSlide( object state )
{
// Do some stuff to show a slide
}

public void StartSlideshow()
{
TimerCallBack tcb = new TimerCallback( ShowSlide );
timer = new Timer( tcb, null, 1000, 1000 );
}
}

Obviously you can change this to be able to specify what time period you
want to leave between slides etc. Any problems post back.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:Oe*************@TK2MSFTNGP15.phx.gbl...
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in

loop
to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay

inbetween
slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak


Nov 17 '05 #6

P: n/a
"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So, its confirmed that we dont have any straight C# keyword or methods for
delay...?
There are lots of ways to delay your code, some good, some bad. Calling
Thread.Sleep is OK if you're not on your UI thread. A better way is to use
the timer. You could vary the speed of the playback simply by adjusting the
timer to fire at longer or shorter intervals.

I thought of going for Threading/timer initially but I wants to try
something else than this.
Any particular reason?

[Snip] I dont know how to solve this delaying...I mean i dont want to push this
function into threading or timer event or loop delay....let see if any one
proceeds this DELAY without delayinnnnnnnn......


What have you got against Threading, Timers and loops? If you want to delay
your code, you have got basically 2 options; if you're not on your UI
thread, call Sleep. But since you seem to be against using your own threads,
you're almost certainly on your UI thread. Your only option without
explicitly creating a thread is to use the Timer.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #7

P: n/a
This is a bad idea because it justs eats processor cycles that the machine
needs elsewhere.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Aravind" <Ar*****@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
Hi,

To make some delay in c#,use the for Loops,

for(i=0;i<=delay;i++);

Adjust the delay value according to ur delay time.Here delay is a long
int.

"Maheshkumar.R" wrote:
Hi groups,

How i can introduce some milliseconds delay in application. How i can do
achieve this...

let me clearly say...
(1) I'm displaying slices of medical images.
For framerate [2x/4x/6x] - for this i'm loading 2+ slices, 4+ slices in
loop
to have cine impact..-> Ok working
(2) But if i want to reduce the speed...how i can introduce delay
inbetween
slices..[ -2x/-4x/-6x]...-> ?????

I'm not using Threading..so i cannot think of Sleep.

Maheshkumar.R
http://spaces.msn.com/members/cyberiafreak


Nov 17 '05 #8

P: n/a
How about UI Thread...? I mean I have to create 'n' instances of forms in
'n' thread.
Will threading works for UI like Forms ...becoz my application is in
finishing stage....now got struck becoz of this delay probs.

Mahesh
"Tim Haughton" <ti*********@gmail.com> wrote in message
news:GV******************@fe05.news.easynews.com.. .
"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So, its confirmed that we dont have any straight C# keyword or methods for delay...?
There are lots of ways to delay your code, some good, some bad. Calling
Thread.Sleep is OK if you're not on your UI thread. A better way is to use
the timer. You could vary the speed of the playback simply by adjusting

the timer to fire at longer or shorter intervals.

I thought of going for Threading/timer initially but I wants to try
something else than this.
Any particular reason?

[Snip]
I dont know how to solve this delaying...I mean i dont want to push this
function into threading or timer event or loop delay....let see if any one proceeds this DELAY without delayinnnnnnnn......


What have you got against Threading, Timers and loops? If you want to

delay your code, you have got basically 2 options; if you're not on your UI
thread, call Sleep. But since you seem to be against using your own threads, you're almost certainly on your UI thread. Your only option without
explicitly creating a thread is to use the Timer.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Nov 17 '05 #9

P: n/a

"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So, its confirmed that we dont have any straight C# keyword or methods for
delay...?

I thought of going for Threading/timer initially but I wants to try
something else than this.

Here is my exact requirement :
As you all know fastforward in movie right..? 2x , 4x, 6x , 8x etc in VCD
player.
I have achieved fastforward by adding 'n+' slices..but when i comes to
Delaying i mean -2x , -4x, -6x etc...

I dont know how to solve this delaying...I mean i dont want to push this
function into threading or timer event or loop delay....let see if any one
proceeds this DELAY without delayinnnnnnnn......


You don't have to push anything into threading, all you have to do is call
Thread.Sleep( delay); and the curent thread will suspend for delay
miliseconds (but not less than 10 or 15 or even more milliseconds).

Willy.
Nov 17 '05 #10

P: n/a
Ok, let me try this now...and write you back..thankz willy

Mahesh
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
So, its confirmed that we dont have any straight C# keyword or methods for delay...?

I thought of going for Threading/timer initially but I wants to try
something else than this.

Here is my exact requirement :
As you all know fastforward in movie right..? 2x , 4x, 6x , 8x etc in VCD player.
I have achieved fastforward by adding 'n+' slices..but when i comes to
Delaying i mean -2x , -4x, -6x etc...

I dont know how to solve this delaying...I mean i dont want to push this
function into threading or timer event or loop delay....let see if any one proceeds this DELAY without delayinnnnnnnn......


You don't have to push anything into threading, all you have to do is call
Thread.Sleep( delay); and the curent thread will suspend for delay
miliseconds (but not less than 10 or 15 or even more milliseconds).

Willy.

Nov 17 '05 #11

P: n/a
"Maheshkumar.R" <ma*********@sninform.com> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
How about UI Thread...? I mean I have to create 'n' instances of forms in
'n' thread.
Will threading works for UI like Forms ...becoz my application is in
finishing stage....now got struck becoz of this delay probs.


I'm not entirely sure what you mean. Your UI will run on a thread. If you
run all of your code on the same thread as the UI, whenever your code is
'doing something', your UI will become unresponsive. It's good practice to
have your 'worker' code on a different thread to your UI *if* your 'worker'
code is doing something non-trivial.

If you're close to delivery and don't understand threading, I think you'd be
advised to talk to your project manager (et.al) and highlight the problem,
the solution, and the risk / delay associated with it.

Applications with a GUI that become unresponsive whenever the program is
doing something invariably look amateurish.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.