Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
|
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
 
Old April 3rd, 2008, 05:19 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default 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.
 
Old April 3rd, 2008, 08:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
 
Old April 4th, 2008, 09:29 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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. :=[
 
Old April 4th, 2008, 10:07 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old April 4th, 2008, 11:39 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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
 
Old April 4th, 2008, 11:45 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========
 
Old April 4th, 2008, 01:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
 
Old April 7th, 2008, 03:38 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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?
 
Old April 7th, 2008, 04:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
 
Old April 7th, 2008, 04:41 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
sleep in xsl sunnn XSLT 1 July 2nd, 2008 12:22 PM
Which Thread to use and How peace2007 ASP.NET 1.0 and 1.1 Professional 0 September 23rd, 2007 03:24 AM
threads: diferences between sleep and spinwait... gbianchi .NET Framework 2.0 2 September 21st, 2007 09:27 AM
vba access something like sleep ... but not quite. oca.cheleu Access VBA 1 September 5th, 2006 07:07 AM
How to Pause or sleep the background worker thread mks033 C# 2005 0 May 3rd, 2006 07:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.