Results 1 to 5 of 5

Thread: How to sleep a process until an event happen.

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    3
    I’m working on a project for the University, on process synchronization and I need to sleep a process until an event happen.
    I did something like this but it didn’t work

    Dim A as Boolean
    ----------------------
    Sub Wait ()
    A= false
    While not(A)
    doevents
    Wend
    EndSub
    ----------------------
    Sub Signal()
    A= true
    EndSub

    Thank you very much

    Sebastian

  2. #2
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    i did basically the same code as
    you have above and it worked...
    i am not sure but i think it would
    depend on what events you connected
    them to...

    Code:
    Dim A As Boolean
    
    Private Sub Command1_Click()
        A = False
        While Not (A)
            DoEvents
        Wend
        MsgBox "Hello!"
    End Sub
    
    Private Sub Command2_Click()
        A = True
    End Sub

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    What kind of "event" do you need to wait for?

    If what you need is to wait for an app to finish loading or until an app has been closed you could try this:

    Wait until an app is loaded
    Code:
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFF
    
    Private Declare Function WaitForInputIdle _
                    Lib "user32" _
                    (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle _
                    Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess _
                    Lib "kernel32" _
                    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                    ByVal dwProcessId As Long) As Long
    
    Private Sub cmdRunApp_Click()
       Dim hProcessID As Long
       Dim hProcess As Long
       Dim sShellPath As String
       
       sShellPath = "C:\WINDOWS\NOTEPAD.EXE"
       
       hProcessID = Shell(sShellPath, vbNormalFocus)
       
       If hProcessID <> 0 Then
          hProcess = apiOpenProcess(SYNCHRONIZE, 0, vhProcID)
          
          If hProcess <> 0 Then
             Call apiWaitForInputIdle(hProcess, INFINITE)
             Call apiCloseHandle(hProcess)
          End If
       
       End If
    
    End Sub

    Wait until an app is closed
    Code:
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFF
    
    
    Private Declare Function WaitForSingleObject _
                    Lib "kernel32" _
                    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle _
                    Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess _
                    Lib "kernel32" _
                    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                    ByVal dwProcessId As Long) As Long
    
    Private Sub cmdRunApp_Click()
       Dim hProcessID As Long
       Dim hProcess As Long
       Dim sShellPath As String
       
       sShellPath = "C:\WINDOWS\NOTEPAD.EXE"
       
       hProcessID = Shell(sShellPath, vbNormalFocus)
       
       If hProcessID <> 0 Then
          hProcess = apiOpenProcess(SYNCHRONIZE, 0, vhProcID)
          
          If hProcess <> 0 Then
             Call Me.Hide
             App.TaskVisible = False
             Call apiWaitForSingleObject(hProcess, INFINITE)
             Call apiCloseHandle(hProcess)
             App.TaskVisible = True
             Call Me.Show
          End If
       
       End If
    
    End Sub

    To put an app to sleep
    Code:
    Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    Just specify the number of seconds you want the app to wait in the dwMilliseconds parameter.

    Et Voila!
    Hope it helps.

  4. #4
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    Oops! A copy and paste mistake.

    Change vhProcID to hProcessID in the first two examples.

    Sorry if it caused any confusion.


    PS.
    Anybody know why I can't Edit a response anymore?

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    3
    Thank you very much for your help.
    It is not for running an application, its a Buffer class, that use a Monitor to solve Producer and Consumer problem, and the Monitor use a Semaphores for synchronisation, and the Semaphores use the Signal and Wait functions,
    So when consumer ask for data to the Buffer it call the Semaphores and it call a Wait if the buffer is empty, and when the producer put data in de buffer it calls the Semaphores and it call Signal that wake up all the cascade process.
    The problem is that it works with, a single Producer, single Consumer but it didn't work when you have some many instances of the buffer, Ex : The first instance of the SignalWait class is stopped forever by the second instance of it.

    Thank you very much for your help, again.

    Sebastian


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