|
-
Jan 11th, 2008, 02:31 AM
#1
Thread Starter
Addicted Member
What does 'sleep' function do?
With reference to my previous thread on copying USB drive
http://www.vbforums.com/showthread.php?t=503894
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
I was told that the 'sleep' function pauses the application for specified time interval. (May not necessarily give time to other processes to run simultaneously ).
Is it true?
So what is the difference between 'sleep' and this loop?
Code:
Dim date1 As Date
date1 = Now
While (DateDiff("s", date1, Now) < 5)
Wend
My main concern is to let my application give some breathing time to other processes while copying folders of several gig in size
Last edited by winterslam; Jan 11th, 2008 at 02:36 AM.
-
Jan 11th, 2008, 03:25 AM
#2
Re: What does 'sleep' function do?
Sleep will tstop the processing on your thread. While that loop will "simulate " sleep but it will also lock up the thread from processing but not if you place a DoEvents in it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 11th, 2008, 05:09 AM
#3
Re: What does 'sleep' function do?
So basically Sleep is what gives the processing time to everything else that is running, as your process is halted and not processed until the specified time has passed.
DoEvents causes message queue to be processed, thus the result it gives within a heavy loop is that your application's windows get drawn, mouse and keyboard messages are triggered if there are any in the queue etc. - thus preventing your application from getting a locked up feeling.
If you have a window open, you don't want to sleep too long. You may catch a cold... err... I mean, your application gets unresponsive. So you probably want to create a loop that first calls Sleep for a little while, then calls for DoEvents to make sure your windows are drawn etc. and then just go back to Sleep again, all the way until you know your other processing is done.
-
Jan 11th, 2008, 07:16 AM
#4
Thread Starter
Addicted Member
Re: What does 'sleep' function do?
Below is my code. The application runs in background. No Form..
The function copies the source folder to the destination folder.
I don't mind if the program takes 1 hour to copy a folder instead of 10 minutes as long as it gives time to other processes on the system
Is the placement of DoEvents correct?
Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim fso As New FileSystemObject
Public Sub FolderCopy(ByVal source As String, ByVal destination As String)
Dim objfile As File
Dim objfolder As Folder
DoEvents
For Each objfile In fso.GetFolder(source).Files
DoEvents
On Error Resume Next
If objfile.Name <> "" Then
objfile.Copy destination & "\" & objfile.Name, True
Sleep 100
End If
Next
For Each objfolder In fso.GetFolder(source).SubFolders
DoEvents
On Error Resume Next
If Dir(destination & "\" & objfolder.Name) = "" Then MkDir destination & "\" & objfolder.Name
FolderCopy objfolder.Path, destination & "\" & objfolder.Name
Next
End Sub
Last edited by winterslam; Jan 11th, 2008 at 07:29 AM.
-
Jan 11th, 2008, 07:25 AM
#5
Re: What does 'sleep' function do?
If you have no form then using DoEvents doesn't serve you any purpose. DoEvents only handles messages of your application.
-
Jan 11th, 2008, 12:33 PM
#6
Re: What does 'sleep' function do?
 Originally Posted by Sleep
The Sleep function suspends the execution of the current thread for a specified interval.
· dwMilliseconds
Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.
 Originally Posted by SleepEx
The SleepEx function causes the current thread to enter a wait state until one of the following occurs:· An I/O completion callback function is called· An asynchronous procedure call (APC) is queued to the thread.· The time-out interval elapses
· dwMilliseconds
Specifies the time, in milliseconds, that the delay is to occur. A value of zero causes the function to return immediately. A value of INFINITE causes an infinite delay.
· bAlertable
Specifies whether the function may terminate early due to an I/O completion callback function or an APC. If bAlertable is FALSE, the function does not return until the time-out period has elapsed. If an I/O completion callback occurs, the function does not return and the I/O completion function is not executed. If an APC is queued to the thread, the function does not return and the APC function is not executed.
If bAlertable is TRUE and the thread that called this function is the same thread that called the extended I/O function (ReadFileEx or WriteFileEx), the function returns when either the time-out period has elapsed or when an I/O completion callback function occurs. If an I/O completion callback occurs, the I/O completion function is called. If an APC is queued to the thread (QueueUserAPC), the function returns when either the timer-out period has elapsed or when the APC function is called.
Maybe these will help clear things up.[/color]
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 11th, 2008, 01:15 PM
#7
Re: What does 'sleep' function do?
 Originally Posted by Merri
... DoEvents only handles messages of your application.
It handles all Windows messages. From Help:
DoEvents switches control to the operating-environment kernel. Control returns to your application as soon as all other applications in the environment have had a chance to respond to pending events. This doesn't cause the current application to give up the focus, but it does enable background events to be processed.
-
Jan 11th, 2008, 09:01 PM
#8
Thread Starter
Addicted Member
Re: What does 'sleep' function do?
final question..
Is the 'sleep' keyword in my code necessary at all?
-
Jan 13th, 2008, 05:16 AM
#9
Re: What does 'sleep' function do?
No its not but if you use the File Operations APIs to use the build in Windows File Copy process and show a dialog with a progressbar it should give the duration / progress of the copy at least.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|