Best way to wait for a process to appear?

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Best way to wait for a process to appear?

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I'm launching it myself.

Thank You PB, I'll give it a try. :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Don't forget to make "exe" a global variable, so you can use it for the PID.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply