How can you check if an exe is already running?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
a-revuser
Posts: 23
Joined: Fri Dec 04, 2009 10:23 am

How can you check if an exe is already running?

Post by a-revuser » Sun Sep 19, 2010 12:23 pm

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?

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

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

Post by shadowslash » Sun Sep 19, 2010 3:57 pm

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
Parañaque, Philippines
Image
Image

a-revuser
Posts: 23
Joined: Fri Dec 04, 2009 10:23 am

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

Post by a-revuser » Tue Sep 21, 2010 1:17 pm

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.

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

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

Post by shadowslash » Tue Sep 21, 2010 3:25 pm

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
Parañaque, Philippines
Image
Image

Post Reply