Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if program is running 1

Status
Not open for further replies.

SitesMasstec

Technical User
Sep 26, 2010
502
BR
Hello Colleagues!

I have an executable program (MENU0REC.EXE) that in its menu has a button to call another program
Code:
RUN /N MENU1EST.EXE

All programs are in VFP 9.

That said, the user execute MENU0REC.EXE, click on one button to execute MENU1EST.EXE and do his/her work. After some time he/she go out for a coffee with the program MENU1EST.EXE opened (running).

When the user get back to work the program is minimize and he/she forgot about it and executes (again!) the main program MENU0REC.EXE.

Is there a way to, when executing MENU0REC.EXE, it checks if there is another instance of MENU0REC.EXE or MENU1EST.EXE in the same computer?


Thank you,
SitesMasstec
 
Hello Chriss!

Ok, I saw the code, thank you.

It seems complicated but I will try to understand it. I have to know where in the code I have to put the 2 program names (MENU0REC.EXE and MENU1EST.EXE).

Also, I want to detect if one of the programs is already running (minimized) in the SAME computer, not in another computer.



Thank you,
SitesMasstec
 
No worries, this is in the same computer.

You want to knwo if MENU1EST.EXE already runs, so you would test for that one. And there are many ways to use this.
Either you check whether MENU1EST.EXE already runs in MENU0REC.EXE and then don't run it again.
Or in the start of MENU1EST.EXE this checks whether itself already runs. If it finds itself twice in the list, it can quit.

Chriss
 
Hi SitesMasstec.

1) Chris' solution is great. 2) The code bellow works well too, and 3) you can always low-level read the tasks from the Windows Task Manager and determine if the exe is already running.

***********************************************************************************


*====== main prg
* Make sure that PROG1.EXE is NOT already running
my_app="PROG1.exe"
run_count=IsExeRunning(my_app)
IF run_count > 1
* if run_count is >1, then the app is already running, so do not run again
* count=1 is original app
* count=2 is the next instance that would find it already running
MESSAGEBOX("The PROG1 is Already running. Press <ENTER>.",48)
* QUIT and switch to the other instance
ENDIF

*==============================================

* Program: IsExeRunning
* Purpose: to determine if an EXE is already running, and terminate it if directed so.

PARAMETERS tcName
local loLocator, loWMI, loProcesses, loProcess
loLocator = createobject('WBEMScripting.SWBEMLocator')
loWMI = loLocator.ConnectServer()
loWMI.Security_.ImpersonationLevel = 3 && Impersonate

loProcesses = loWMI.ExecQuery([SELECT * FROM Win32_Process WHERE Name = '] + tcName + ['])

return loProcesses.count
*---------------------------------

***********************************************************************************

Good luck,
Sime
 
Chris, Sime:

As I am not an expert in Visual FoxPro I opted to dive in Sima's code (because the few lines it has) and it worked for me.

Sima, I would like to know if the code you provided allows the same program to run in 2 different machines in a network.

All I need is the program NOT running twice in the SAME machine, but it needs to be run by users in different machines.


Thank you,
SitesMasstec
 
Sure, it can run on as many machines as you want (on a network location or their separate copies on the local drives), files shared, handshaking and all that. It just doesn't allow it to run twice from the same PC/workstations - which is what you want, right?
 
Just one thing, SitesMasstec

your question is valid, as WMI in general can query for other systems, too.
But both solutions are based on the local process list only. So only on the same computer. It takes a bit more to run WMI queries that search in many computers.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top