Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep function

Status
Not open for further replies.

testare

Programmer
Jan 21, 2005
127
Hi,

I wanna make my program do nothing for 45 min. because my program should wait for another program to finish and it takes the other program about 45 min to be done.

Should i use
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
or should i use a timer?

 
What has that link do with my question?
 
I don't think it really makes a difference in your case. Are you start the program that takes 45 min from your program? If so, you could probably do a search on executing a program and waiting for it to complete. If not, perhaps you could use a file that would be checked occasionally just to make sure the other program finished. Otherwise, just pick your poison - both the timer and sleep sound ok to me.
-Max
 
If your app is causing the other program to start, then you should look at this faq faq222-428


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi shakespeare5677,

My program should not be visible to the user.

If i use Sleep (2700000) or Timer1 there is no different between them?
Example like sleep take more memory or timer does, or something like that.

If i use sleep will it work fine?

/Testare
 
I wouldn't neccesarily use either for this.

From your description of the what you want to achieve I'd probabaly consider a ShellAndWait solution (just do a keyword search in this forum).



 
I agree with strongm if your app starts the one you are monitoring. Otherwise, there is no difference no noticable difference on resources (as far as I know), for technical differences you'd have to ask the pros.
-Max
 
Last Question, I promise...

The other program is not my program it's another program that i have not access to.
That other program never ends. It is always up an running in windows.

For these 45 min that i want my program to wait, that other program will work hard against the MS-SQL database, but after these 45 has ended that other program stop working hard with the database and thats when i want my program to begin work.

Both program cannot work att the same time with the DB

So my question is sleep
Code:
Sleep (2700000)
a good way?

Thanks for all the replies.

/Testare
 
No, not in my opinion. If you are going to use sleep then use it in a loop along with doevents (this ensures a certain amount of responsiveness in your program that will otherwise be lacking, e.g. being able to cancel or close it ...) whilst keeping resource usage to a minimum

Another (somewhat more complicated) technique is to use the MsgWaitForMultipleObjects API call in a loop; this has been illustrated a few times in the forum. A keyword search should locate something.
 
How should i use sleep in a loop?

Could you help me with that?

 
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
vbWait 5000
End Sub

Private Sub vbWait(ByVal dwMilliseconds As Long, Optional ByVal dwGranularity As Long = 250)
Dim lp As Long
For lp = 1 To dwMilliseconds Step dwGranularity
Sleep dwGranularity
Next
End Sub
 
strongm: where is the DoEvents? :)

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Oops ... see what happens when you just try and slap something together quickly ...


Yes, there should be a DoEvents immediately after the Sleep. So the code should read:

Code:
[blue]Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    vbWait 5000
End Sub

Private Sub vbWait(ByVal dwMilliseconds As Long, Optional ByVal dwGranularity As Long = 250)
    Dim lp As Long
    For lp = 1 To dwMilliseconds Step dwGranularity
        Sleep dwGranularity
        [b]DoEvents[/b]
    Next
End Sub[/blue]

As ever, remember that this is an illustrative example rather than a rigorous solution (e.g. this version will only give you a delay of exactly the required length if dwGranularity is a factor of dwMilliseconds)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top