 |
| Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 3rd, 2008, 05:19 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
How do you put a thread to sleep?
Code:
Public Sub Main()
Do While True
If TheresSomethingToDo Then
Do_It()
End If
Thread.Sleep(For5Seconds)
Loop
End Sub
The idea is to have an app stay loaded, but without putting anything on the screen. Regularly, it polls to see if there is a task to be done. If not, back out of tying up the processor, but don't unload. After some time, wake back up (which means now being active in the Windows Loop, etcâthe things an active app does) see if there is something to do now.
There would be added a way to exit the loop, so as to end the application.
|
|

April 3rd, 2008, 08:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Brian are you asking something here or this is a statement?? since it look that you have the code already.. ;)
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

April 4th, 2008, 09:29 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
The question is in the title of the post âHow do you put a thread to sleep [in VB6]â, and the code I posted is merely pseudo code.
VB6 doesn't actually have a Thread.Sleep() method.
Sorry I wasn't a little clearer. :=[
|
|

April 4th, 2008, 10:07 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Hi Brian,
It's been a terribly long time since I have worked with VB6 but I *think* you can get a hook onto kernel32 and then sleep the thread from there.
-Doug
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
|
|

April 4th, 2008, 11:39 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
The following worked great. I opened TaskMan, and went to the Processes tab so that I could watch how much CPU % the app used. It showed 00 for 5 seconds, then about 2 Secs of 95 to 99% CPU usage. After it did this 20 times, the app quit.
Code:
Option Explicit
Private Declare Sub Sleep _
Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub Main()
Dim s As Single
Dim i As Integer
For i = 1 To 20 ' Repeat this stuff 20 Ã
Sleep 5000 ' Sleep for 5 sec.s
s = Timer
Do Until Timer >= s + 2 ' Do stuff for 2 sec.s
DoEvents
Loop
Next i
End Sub
|
|

April 4th, 2008, 11:45 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Glad it worked out for you Brian. =]
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
|
|

April 4th, 2008, 01:37 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Brian, do you remember that you can't have more than one thread running in VB6, don't you?
so if you sleep, you are sleeping all the system, even if you have timers, they will sleep too (well, they will still be running, but no one will proccess the events until the sleep is done)..
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

April 7th, 2008, 03:38 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
OK. Now Iâm worried.
I want to have this program running on a server that is running an internet server. I want this .exe to start when the machine is started, to routinely check for the presence of a specific kind of file in a predetermined location, to process the file if found, and then to delete it. Since checking every 5 seconds seems just about perfect, I want the app to sleep in between.
So now you are telling me that if I sleep a vb app that was started this way Iâll sleep the machine? (This canât be right.)
My use of Sleep was to [u]replace</u> my use of SetTimer. Perhaps what you meant was using Sleep and SetTimer in the same app is problematic because Sleep would cause that one apllicationâs timer to be ignored or stopped...
Could you clarify?
|
|

April 7th, 2008, 04:06 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
NO NO.. my mistake, sorry, you will sleep your program (not all your system, sorry again), so your program will not receive any event till the sleep finish. SO SORRY AGAIN!!!. For example you can't proccess any event (like a timer ;), while you are sleep.)
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

April 7th, 2008, 04:41 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Right. Your VB application will be single threaded so, what Gonzalo is saying, when you sleep the thread in your loop anything that may also be executing on that thread will also sleep. The timer example that Gonzalo poses fits the example nicely.
Don't worry, you are not going to put the entire server to sleep...well not without a lot of deliberate work on your end! =]
-Doug
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
|
|
 |