Page 1 of 1

Best way to wait for a process to appear?

Posted: Sat Jul 18, 2009 3:17 pm
by SFSxOI
I want to perform a function (set the thread priority) for a particular process thread but I need to wait for that process to appear before doing so, then I'll get the process ID. After getting the process ID I will then set the thread priority.

What is the most reliable and best way to wait for that process to appear?

I already know how to get the PID, I just need a good way to wait for it. I know I probably need to do some type of loop or something, but whats the best way to do that? Wait, Until, etc....? I really need it to be very light on CPU time and very small memory foot print wise.

Re: Best way to wait for a process to appear?

Posted: Sat Jul 18, 2009 3:37 pm
by PB
Are you launching the process yourself, or is it a third-party app?
If yourself, then for Windows (not Linux/Mac) I've been using this:

Code: Select all

Procedure RunAndWaitTillReady(exe$)
  exe=RunProgram(exe$,"",GetPathPart(exe$))
  If exe : WaitForInputIdle_(exe,#INFINITE) : Sleep_(250) : EndIf
EndProcedure

RunAndWaitTillReady("notepad.exe")

keybd_event_(#VK_O,0,0,0) : keybd_event_(#VK_O,0,#KEYEVENTF_KEYUP,0)
keybd_event_(#VK_K,0,0,0) : keybd_event_(#VK_K,0,#KEYEVENTF_KEYUP,0)
Note: The Sleep_(250) seems to be obsolete, but in my tests it's needed,
just to give the launched app a quarter of a second longer to get ready
than it reports. That is: without it, the text doesn't always go to Notepad.

Posted: Sat Jul 18, 2009 3:51 pm
by SFSxOI
I'm launching it myself.

Thank You PB, I'll give it a try. :)

Posted: Sat Jul 18, 2009 4:10 pm
by PB
Don't forget to make "exe" a global variable, so you can use it for the PID.