Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Wait for Process to start?

Posted on 2003-03-14
Medium Priority
Last Modified: 2010-04-07
Is there a way to check if an application process has started. I know there are code available to wait for a process to end but is there a way to wait for a process to start?
Question by:gunaveluj1234
  • 4
  • 3

Expert Comment

ID: 8145527
Please define start. Do you mean a seperate process that was spawned (or shelled) by the main program? And by start do you mean it shows up as a "running" process?

Expert Comment

ID: 8145557
Here is a quick way to list all of the currently running processes, so you could use this, keep looping until the process you just started appears in the list... code to follow.

Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260

    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * MAX_PATH
End Type

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

Dim ListOfActiveProcess() As PROCESSENTRY32

Public Function szExeFile(ByVal Index As Long) As String
    szExeFile = ListOfActiveProcess(Index).szExeFile
End Function

Public Function dwFlags(ByVal Index As Long) As Long
    dwFlags = ListOfActiveProcess(Index).dwFlags
End Function

Public Function pcPriClassBase(ByVal Index As Long) As Long
    pcPriClassBase = ListOfActiveProcess(Index).pcPriClassBase
End Function

Public Function th32ParentProcessID(ByVal Index As Long) As Long
    th32ParentProcessID = ListOfActiveProcess(Index).th32ParentProcessID
End Function

Public Function cntThreads(ByVal Index As Long) As Long
    cntThreads = ListOfActiveProcess(Index).cntThreads
End Function

Public Function thModuleID(ByVal Index As Long) As Long
    thModuleID = ListOfActiveProcess(Index).th32ModuleID
End Function

Public Function th32DefaultHeapID(ByVal Index As Long) As Long
    th32DefaultHeapID = ListOfActiveProcess(Index).th32DefaultHeapID
End Function

Public Function th32ProcessID(ByVal Index As Long) As Long
    th32ProcessID = ListOfActiveProcess(Index).th32ProcessID
End Function

Public Function cntUsage(ByVal Index As Long) As Long
    cntUsage = ListOfActiveProcess(Index).cntUsage
End Function

Public Function dwSize(ByVal Index As Long) As Long
    dwSize = ListOfActiveProcess(Index).dwSize
End Function

Public Function GetActiveProcess() As Long
    Dim hToolhelpSnapshot As Long
    Dim tProcess As PROCESSENTRY32
    Dim r As Long, i As Integer
    hToolhelpSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    If hToolhelpSnapshot = 0 Then
        GetActiveProcess = 0
        Exit Function
    End If
    With tProcess
        .dwSize = Len(tProcess)
        r = ProcessFirst(hToolhelpSnapshot, tProcess)
        ReDim Preserve ListOfActiveProcess(20)
        Do While r
            i = i + 1
            If i Mod 20 = 0 Then ReDim Preserve ListOfActiveProcess(i + 20)
            ListOfActiveProcess(i) = tProcess
            r = ProcessNext(hToolhelpSnapshot, tProcess)
    End With
    GetActiveProcess = i
    Call CloseHandle(hToolhelpSnapshot)
End Function


Author Comment

ID: 8154326
Is there any event driven mechanism to wait for the process rather than waiting in a loop. Waiting in a loop is using 100% cpu resources.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8154343
you could have the "launched" program send a system message that you have registered to receive.

Author Comment

ID: 8154377
Problem is i do not have access to the source code of that application. It is a third-party application. Is there any other way to do this?

Accepted Solution

Shaka913 earned 90 total points
ID: 8154425
Well if Process starting always creates a window, you could set a system hook and look for that particular window to start using WH_SHELL hooks. otherwise I think you just spawn it, loop and wait. Sorry not any easier.

Author Comment

ID: 8155586
Thanks, i was able to create a hook and then find out when the application starts up.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question