How to introduce delay in C#..

  • Thread starter Thread starter Maheshkumar.R
  • Start date Start date
M

Maheshkumar.R

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
 
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.
 
No aravind. I know this shortcut. But I'm looking something in Keyword
perspective or any inbuilt function from C#..

Mahesh


Aravind said:
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 said:
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
 
Aravind said:
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.
 
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
 
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 said:
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 said:
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
 
Maheshkumar.R said:
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
 
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 said:
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 said:
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
 
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 said:
Maheshkumar.R said:
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
 
Maheshkumar.R said:
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.
 
Maheshkumar.R said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top