Results 1 to 9 of 9

Thread: What does 'sleep' function do?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    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.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: What does 'sleep' function do?

    Quote Originally Posted by Sleep
    The Sleep function suspends the execution of the current thread for a specified interval.

    &#183; 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.
    Quote Originally Posted by SleepEx
    The SleepEx function causes the current thread to enter a wait state until one of the following occurs:&#183; An I/O completion callback function is called&#183; An asynchronous procedure call (APC) is queued to the thread.&#183; The time-out interval elapses

    &#183; 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.

    &#183; 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: What does 'sleep' function do?

    Quote 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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: What does 'sleep' function do?

    final question..


    Is the 'sleep' keyword in my code necessary at all?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width