CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2005
    Posts
    32

    Is there a function better than API SLEEP?

    I have read that from the internet that API sleep minimum delay is 10ms even though it is "sleep 1".

    Is there a function that is almost like the API sleep but much more precise? Real 1millisecond delay?

    I have seen some multimedia timer that is able to set the interval to 1ms and it really does. But I really need SLEEP function to do the job.

    Thank you.

  2. #2
    Join Date
    Aug 2003
    Location
    London
    Posts
    515

    Re: Is there a function better than API SLEEP?

    SleepEx??

    Code:
    Declare Function SleepEx Lib "kernel32.dll" (ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
    If it helped, then please rate the post by clicking "Rate this post"!

  3. #3
    Join Date
    Jan 2005
    Posts
    32

    Re: Is there a function better than API SLEEP?

    SleepEx isnt the one that I am looking for.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Is there a function better than API SLEEP?

    Quote Originally Posted by leesing2k3
    I have read that from the internet that API sleep minimum delay is 10ms even though it is "sleep 1".
    A simple test shows me that Sleep(1) on my computer takes approx 1.9ms, but as far as I know thats really up to the hardware. Anyway, in a multithreading environment, calling Sleep(...) suspends the thread for atleast X milliiseconds, and yields the processor to other waiting threads/processes.

    Quote Originally Posted by leesing2k3
    Is there a function that is almost like the API sleep but much more precise? Real 1millisecond delay?
    There is a function in kernel32.dll, called ZwDelayExecution(...) which let you specify the delay in units of 100ns.
    - petter

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Is there a function better than API SLEEP?

    It might be worth pointing out that vb isn't fast enough for one millisecond accuracy. That may be tricky for any language in fact, though I've not looked into it. Many times what seems like a need for this accuracy is really a need to measure the passage of time with precision, not control it. It would probably be worthwhile for you to give some details on what you are trying to accomplish. There are always different approaches, and someone is bound to know of at least one or two that can work for you.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Jan 2005
    Posts
    32

    Re: Is there a function better than API SLEEP?

    Omg, finding information about ZwDelayExecution is very hard.

    Not much information can be found in google or yahoo.

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Is there a function better than API SLEEP?

    It's not in kernel32, it's in ntdll, sorry for that.

    The signature of the function is:

    Code:
    NTSYSAPI NTSTATUS NTAPI ZwDelayExecution(IN BOOLEAN Alertable, IN PLARGE_INTEGER Interval)
    - or more readable -
    Code:
    DWORD __stdcall ZwDelayExecution(BOOLEAN Alertable, __int64* Interval)
    The Alertable specified whether to "sleep" in an alertable state or not.
    The Interval, a pointer to a 64 bit integer, is somewhat 2 sided. If it is negative it specifies the relative time to sleep (in units of 100ns), if not is specifies the absolute time to wake up.

    With my limited knowlege I'm not sure if it's possible to call this function from VB at all.

    But here's some non error-checking C code:
    Code:
    #include <windows.h> 
     
    typedef  DWORD (__stdcall *pfZwDelayExecution)(BOOLEAN, __int64*); 
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        HINSTANCE hinstLib; 
        pfZwDelayExecution ZwDelayExecution; 
     
        // Get a handle to the DLL module.
        hinstLib = LoadLibrary(TEXT("ntdll")); 
     
        // Get the pointer to the function
        ZwDelayExecution = (pfZwDelayExecution) GetProcAddress(hinstLib, "ZwDelayExecution"); 
    
        // sleep for 20 milliseconds
        __int64 x = -200000;
        ZwDelayExecution(FALSE, &x);
     
        // Free the DLL module.
         fFreeResult = FreeLibrary(hinstLib); 
    }
    - petter

  8. #8
    Join Date
    Jan 2005
    Posts
    32

    Re: Is there a function better than API SLEEP?

    Public Declare Sub DelayExecution Lib "ntdll.dll" Alias "ZwDelayExecution" (ByVal Alertable As Boolean, ByVal Interval As Long)

    I do this and I call this

    DelayExecution False, 300000000
    and
    DelayExecution False, -300000000

    and it doesnt seem to delay
    Last edited by leesing2k3; August 10th, 2005 at 09:23 PM.

  9. #9
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Is there a function better than API SLEEP?

    Again, with no understanding of VB, the second argument, Interval is a pointer to an 64 bit integer (AFAIK long is a 32 bit integer).

    If you got a 64 bit integer type (LONG LONG) in VB you could try something like this:
    Code:
    Public Declare Sub DelayExecution Lib "ntdll.dll" Alias "ZwDelayExecution" (ByVal Alertable As Boolean, ByRef Interval As Long Long)
    
    Dim Interval as Long Long
    Interval = -300000000
    DelayExecution False, ByRef Interval
    - petter

  10. #10
    Join Date
    Jan 2005
    Posts
    32

    Anybody know how to call "ZwDelayExecution" from "ntdll.dll"?

    According to wildfrog

    The signature of the function is:
    NTSYSAPI NTSTATUS NTAPI ZwDelayExecution(IN BOOLEAN Alertable, IN PLARGE_INTEGER Interval)
    - or more readable -
    DWORD __stdcall ZwDelayExecution(BOOLEAN Alertable, __int64* Interval)

    This call is almost like the API Sleep but with a delay in units of 100ns.

  11. #11
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Is there a function better than API SLEEP?

    The Currency data type is 64 bits, but supports a decimal point. Still, the question is, why the need for such precision delay times? As stated, vb is just not fast enough even with such an API. You could end up banging you head against a wall on this one. If you can elaborate on what you're doing, we might have some simple alternative suggestions.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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

Featured