Any functionality in PB to allow just one instance of a PB app (.exe)?

1
0
-1

Hello fellows ...

Do you know about any way how to allow just one instance of a PB app to be running at a time? ... this is, if you execute another instance of the same application, it detects and show any kind of warning to advice not to do it ... I would appreciate any help ... Thanks in advance...

Question Tags: 

Answers

Daryl Foster answered Any functionality in PB to allow just one instance of a PB app (.exe)?

1
+2
-1

Hi Carlos, you need to look at the CreateMutex windows API function.  Roland has an example on his website:

http://www.topwizprogramming.com/freecode_mutex.html

 

Chris Pollach's picture

Hi Daryl / Carlos;

   Note that starting in W10, you must use the Unicode version of this API call. When I migrated to PB 12.6 under W10 for the first time, I had to change my framework to use the Unicode version before this feature would work properly / consistently.

FUNCTION uLong   CreateMutex ( uLong lpMutexAttributes,  Boolean bInitialOwner, ref string lpName ) LIBRARY "KERNEL32.dll" ALIAS FOR CreateMutexW

HTH

Regards ... Chris

Michael Hartnett answered Any functionality in PB to allow just one instance of a PB app (.exe)?

1
+2
-1

Hi Carlos,

We used to use Mutex, but I think we changed as it did not detect when the application was running under another user on a Terminal Server.  We switched to use the WbemScripting Windows object and it is a very small amount of code.  It will only work for runtime app, you can still run another copy of the app in PB on the same PC, which is handy for testing.

Try the following:

//Code to check whether Application is already running
OleObject locator, service, app
String ls_query = 'select name , description from Win32_Process where name = "APPNAME.exe" '
int num, ret
locator = CREATE OleObject
ret = locator.ConnectToNewObject("WbemScripting.SWbemLocator");
service = locator.ConnectServer();
app= service.ExecQuery(ls_query);
num = app.count()
 
IF num > 1 THEN
   MessageBox ("Process","Application is already running on this computer!")
   halt
ELSE
   //Code to execute if app not already running!
END IF
//End of Code
 
Hope That Helps
Michael

Roland's picture

That will only work if they are on the same server. Most companies will have a 'server farm' with several terminal/Citrix servers. The best way to detect other instances is to query the database. With Sybase/Microsoft you can query the tables used by the sp_who stored procedure. Just make sure you set the AppName connection parameter when the program connects. Just do a select count by AppName and userid.