1. Carlos Andres Rico
  2. PowerBuilder
  3. Tuesday, 12 June 2018 23:05 PM UTC

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

Michael Hartnett Accepted Answer Pending Moderation
  1. Wednesday, 13 June 2018 07:50 AM UTC
  2. PowerBuilder
  3. # 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
Comment
  1. Roland Smith
  2. Wednesday, 13 June 2018 13:00 PM UTC
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.

  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Wednesday, 13 June 2018 00:14 AM UTC
  2. PowerBuilder
  3. # 2

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

 

Comment
  1. Chris Pollach @Appeon
  2. Wednesday, 13 June 2018 14:41 PM UTC
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

  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.