Thread.Sleep question

  • Thread starter Thread starter Franky
  • Start date Start date
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
 
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.
 
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.
 
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.
 
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.
 
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
 
What could be wrong with Thread.Sleep, except that the app becomes
non-responsive??

Thanks
 
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
 
Back
Top