Page 1 of 1

How can you check if an exe is already running?

Posted: Sun Sep 19, 2010 12:23 pm
by a-revuser
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?

Re: How can you check if an exe is already running?

Posted: Sun Sep 19, 2010 3:57 pm
by shadowslash
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.

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
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:

Code: Select all

on mouseUp
   answer processExists("notepad.exe")
end mouseUp
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. Image

Re: How can you check if an exe is already running?

Posted: Tue Sep 21, 2010 1:17 pm
by a-revuser
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.

Re: How can you check if an exe is already running?

Posted: Tue Sep 21, 2010 3:25 pm
by shadowslash
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.
Thanks! If you still get problems with it, please don't hesitate toask. Image