Thread.Sleep question

F

Franky

From the Doc:

Thread.Sleep (Int32) Suspends the current thread for a specified time.

Thread.Sleep (TimeSpan) Blocks the current thread for a specified time.

Do these have the same effect. That is, do the words "Blocks" and Suspends"
mean the same or different things.



Thanks in advance
 
D

Dick Grier

These are the same.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
F

Franky

Thanks

Are they better to use then
Do Until VB.Now > EndTime

Application.DoEvents()

Loop
 
P

Phill W.

Franky said:
Are they better to use then
Do Until VB.Now > EndTime
Application.DoEvents()
Loop

Yes!!

The above will drive the CPU usage of your machine through the roof,
even though it still allows other processes to run.

Thread.Sleep() actually suspends the current thread, so it uses
[virtually] no CPU.

Regards,
Phill W.
 
F

Franky

Thanks, good to know


Phill W. said:
Franky said:
Are they better to use then
Do Until VB.Now > EndTime
Application.DoEvents()
Loop

Yes!!

The above will drive the CPU usage of your machine through the roof, even
though it still allows other processes to run.

Thread.Sleep() actually suspends the current thread, so it uses
[virtually] no CPU.

Regards,
Phill W.
 
D

Dick Grier

Hi,

The answer, like many is, "This depends (on what you want your application
to do)." However, I'd say, in general, that Sleep should be used.
Application.DoEvents has a lot of overhead, especially in a loop. However,
if you want the message pump in your app to respond to messages during the
delay, then you must use DoEvents (I often put both DoEvents AND Sleep
inside the loop).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
B

Brian Gideon

Franky said:
Thanks

Are they better to use then
Do Until VB.Now > EndTime

Application.DoEvents()

Loop

Franky,

Are you wanting to block a UI thread? If so then this would be less
bad than Thread.Sleep, but it's still a bad idea. Consider an
alternate approach. Can you explain a bit more about your problem?

Brian
 
F

Franky

What could be wrong with Thread.Sleep, except that the app becomes
non-responsive??

Thanks
 
B

Brian Gideon

Franky said:
What could be wrong with Thread.Sleep, except that the app becomes
non-responsive??

Thanks

Well, that is the problem. If called on non-UI thread it would be a
different story.

Brian
 

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

Top