How can you check if an exe is already running?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
How can you check if an exe is already running?
I need to check if a particular exe is already running, because if it is not, I want to terminate the one trying to detect it?
-
- Posts: 344
- Joined: Tue Feb 24, 2009 6:14 pm
- Contact:
Re: How can you check if an exe is already running?
I'm assuming you're using a Windows OS. So here's what I normally do. I use the tasklist console of Windows to retrieve the list of running processes. To make it easier, I made a little function for you to easily incorporate and use on your projects. I included comments so you would know what the bit of code does.
How to use the above function?
Simply paste it into your mainstack and then when you want to check for a process, simply call it with the EXE name as a parameter. An example is:
If you put that code in a button and click it, it will show true if Notepad is running and false if not. If you have more questions regarding this, please don't hesitate to ask. 
Code: Select all
function processExists pProcess
// Declare the variables.
local tTempValueStorage,tProcesses,tProcessResult
// Back up current settings. (So it doesn't interfere with your app if ever.)
put the hideConsoleWindows into tTempValueStorage
// Hide the console windows.
set the hideConsoleWindows to true
// Get the result from Windows' "tasklist" console.
put shell("tasklist /NH /FO CSV") into tProcesses
// Set default return value.
put false into tProcessResult
// Check the results.
set the itemDel to quote
repeat for each line tProcessLine in tProcesses
delete char 1 of tProcessLine
put item 1 of tProcessLine into tProcessLine
if tProcessLine is pProcess then
put true into tProcessResult
exit repeat
end if
end repeat
// Return the old value of the hideConsoleWindows property.
set the hideConsoleWindows to tTempValueStorage
// Output the result.
return tProcessResult
end processExists
Simply paste it into your mainstack and then when you want to check for a process, simply call it with the EXE name as a parameter. An example is:
Code: Select all
on mouseUp
answer processExists("notepad.exe")
end mouseUp

Re: How can you check if an exe is already running?
Hi Shadowslash,
This is most helpful. I will be trying this out very soon. I expect many other users will be pleased with this code snippet as well.
This is most helpful. I will be trying this out very soon. I expect many other users will be pleased with this code snippet as well.
-
- Posts: 344
- Joined: Tue Feb 24, 2009 6:14 pm
- Contact:
Re: How can you check if an exe is already running?
Thanks! If you still get problems with it, please don't hesitate toask.a-revuser wrote:Hi Shadowslash,
This is most helpful. I will be trying this out very soon. I expect many other users will be pleased with this code snippet as well.
