Results 1 to 6 of 6

Thread: [RESOLVED] Why is CreateEvent Not Functioning Correctly

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    [RESOLVED] Why is CreateEvent Not Functioning Correctly

    When I run the code below, the return value from WaitForSingleObjects is 0, despite the fact that I specified in CreateEvent for the initial state to be non-signaled. The code functions correctly if I uncomment the ResetEvent command (resulting in a return value of WAIT_TIMEOUT). The handle from CreateEvent is valid, but is not properly setting the initial state. Does anyone know why this might be happening?

    VB Code:
    1. Public Declare Function ResetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
    2. Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    3. Public Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (ByVal lpEventAttributes As Long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
    4.  
    5. Private Sub test()
    6.    
    7.     Dim hTest As Long
    8.     Dim lngRetVal As Long
    9.  
    10.     hTest = CreateEvent(ByVal 0&, True, False, ByVal 0&)
    11.     'ResetEvent (hTest)
    12.     lngRetVal = WaitForSingleObject(hTest, 0)
    13.    
    14.     If (lngRetVal = 0) Then
    15.          'Code Enters Here
    16.     Else
    17.          'Does Not Enter Here
    18.     End If
    19.  
    20. End Sub
    Last edited by Joey_k29; Feb 5th, 2007 at 11:59 PM.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Why is CreateFile Not Functioning Correctly

    I run your code and it always returns WAIT_TIMEOUT, with or without ResetEvent

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Re: Why is CreateFile Not Functioning Correctly

    Jcis: Are you running VB6 in Windows XP SP2? So far I have only found one other instance on the web where someone had the same question on another forum (that went unanswered). He too was running it in Windows XP SP2.

    I have tested both in the IDE and compiled form. Neither one is working for me. But as best as I can tell (and further confirmed by your success) there is nothing wrong with the code.

    Thank you for your input.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Why is CreateFile Not Functioning Correctly

    Quote Originally Posted by Joey_k29
    Jcis: Are you running VB6 in Windows XP SP2?
    Yes, tested and works fine in 2 computers, both with Windows XP SP2 + VB6.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Re: Why is CreateFile Not Functioning Correctly

    Very odd. I don't know how to explain it. I tested other code where I created an event and reset it in the same function. In a later function, I called WaitForSingleObject with it, and it failed to wait indicating the event was set despite the fact that there was no code to set the event.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Re: Why is CreateFile Not Functioning Correctly

    The problem I encountered is ByVal 0& is not correct as the last parameter in create event. Doing so results in a named event. vbNullString properly results in an unnamed event and solves my issue.

    Thank you for your help.

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