vb: how to wait for a second?

  • Thread starter Thread starter Jurgen Oerlemans
  • Start date Start date
J

Jurgen Oerlemans

I have a code where I perform several actions on files.
Between these actions I want to wait for 1 second. Or 2 seconds.
How can I easily do this without using a timer? Jurgen
 
Jurgen Oerlemans said:
I have a code where I perform several actions on files.
Between these actions I want to wait for 1 second. Or 2 seconds.
How can I easily do this without using a timer? Jurgen

Well, Thread.Sleep will make the current thread pause for a second. You
don't want to do that on a UI thread though...

Out of interest, why do you want to avoid using a timer?
 
well, to be quite frankly I don't know how to use a timer for a simple
wait-statement.
I use one to perform an action every 5 minutes, but don't know how to use
one to pause the program for a second and then proceed....

Also, using thread.sleep gives me also all kinds of errors like
Name 'Thread' is not declared.

Jurgen
 
Jurgen Oerlemans said:
well, to be quite frankly I don't know how to use a timer for a simple
wait-statement.
I use one to perform an action every 5 minutes, but don't know how to use
one to pause the program for a second and then proceed....

Well, that depends on the kind of timer you're using.

See http://www.pobox.com/~skeet/csharp/multithreading.html#timers
Also, using thread.sleep gives me also all kinds of errors like
Name 'Thread' is not declared.

That suggests you haven't imported the System.Threading namespace.
 
Hi John,

When you use a timer, you need to test that in your program logic.
The threading.thread.sleep is very good for this in my opinion, because it
needs no extra program logic.

Cor
 
Cor Ligthert said:
Threading.thread.sleep(1000)

Let the main thread sleep 1 second.

Very slight clarification: it makes the *current* thread sleep for one
second, whether or not this is the "main" thread. You can't make other
threads sleep.
 
Thank you very much helping this new newbie!
Threading.thread.sleep(1000) does the job!

Jurgen
 
Cor Ligthert said:
When you use a timer, you need to test that in your program logic.
The threading.thread.sleep is very good for this in my opinion, because it
needs no extra program logic.

It entirely depends on what you're trying to do. If you want something
to happen in the UI thread in a second, then blocking the UI thread for
a second is a nightmare. If you're just waiting to retry something on a
worker thread, it's absolutely fine.
 
It entirely depends on what you're trying to do. If you want something
to happen in the UI thread in a second, then blocking the UI thread for
a second is a nightmare. If you're just waiting to retry something on a
worker thread, it's absolutely fine.
No disagrement about this,

Cor
 
Threading.thread.sleep(1000)
Very slight clarification: it makes the *current* thread sleep for one
second, whether or not this is the "main" thread. You can't make other
threads sleep.

Correct
 
Back
Top