Welcome, Guest.
Please login or register.
Single Instance Program
Forum Login
Login Name: Create a new account
Password:     Forgot password

XBLite Forum    General Boards    XBLite Programming  ›  Single Instance Program
Users Browsing Forum
No Members and 1 Guests

Single Instance Program  This thread currently has 3,783 views. Print Print Thread
1 Pages 1 Recommend Thread
Carry
October 22, 2013, 1:37am Report to Moderator Report to Moderator
Baby Member
Posts: 36
I'd like to write a program that prevents more than one instance of itself running at one time. Maybe it's even possible to have the original program react if it detects a second instance trying to start? Like for example, it could trigger the original program window to come to the foreground.

Where can I find xblite code that does something similar to this?
Logged Offline
Private Message Private message
XBLiteAdmin
October 22, 2013, 7:49am Report to Moderator Report to Moderator
Administrator Group
Posts: 44
One way to check for a single instance is to create a unique mutex, then check to see if
the mutex has already been assigned (untested code below!):

In PROLOG, IMPORT kernel32.dec

' InstanceTest returns 0 if instance exists or cannot create mutex. otherwise returns 1.
FUNCTION InstanceTest ()
   UniqueName$ = "MySingleInstaceName"                     '
   IF CreateMutex (0, 0, &UniqueName$) = 0 OR GetLastError() = $$ERROR_ALREADY_EXISTS THEN
      RETURN 0
   ELSE
      RETURN 1
   END IF
END FUNCTION

Logged Offline
Site Site Private Message Private message Reply: 1 - 4
Carry
October 22, 2013, 10:52pm Report to Moderator Report to Moderator
Baby Member
Posts: 36
Thanks very much! I got this to work.

IF CreateMutexA (0, 0, &UniqueName$) = 0 || GetLastError() = $$ERROR_ALREADY_EXISTS THEN
Logged Offline
Private Message Private message Reply: 2 - 4
Guy1954
December 17, 2013, 2:10pm Report to Moderator Report to Moderator

Medium Member
Posts: 162
Hi Carry,

In viXen, I handle the single instance requirement with the code:
  '
  '=====================================================================
  ' To prevents more than one instance running at one time,
  ' event "..._is_active" is created the first time the program is run
  ' to signal this running instance to later launches.
  '=====================================================================
  '
  sEventActive$ = PROGRAM$ (0) + "_is_active"
  SetLastError (0)
  hEventActive = CreateEventA (0, 0, 0, &sEventActive$)
  errNum = GetLastError ()
  IF errNum = $$ERROR_ALREADY_EXISTS THEN
    msg$ = "StartUp: Abnormal end: " + PROGRAM$ (0) + ".exe is already active"
    XstAbend (msg$)      ' terminate current application
  ENDIF

Bye! Guy
Logged Offline
Site Site Private Message Private message Reply: 3 - 4
Guy1954
December 30, 2013, 1:29pm Report to Moderator Report to Moderator

Medium Member
Posts: 162
Hi Xbliters,

D. is right, a mutex is indeed the way to go:

IMPORT "kernel32"   ' operating system

' make sure only one instance of viXen is active at a time
SetLastError (0)
hMutex = CreateMutexA (0, 0, &"viXen")
IFZ hMutex THEN
     errNum = $$ERROR_ALREADY_EXISTS
ELSE
     errNum = GetLastError ()
ENDIF
IF (errNum == $$ERROR_ALREADY_EXISTS) THEN XstAbend ("Application terminated: viXen is already active.")

Bye! Guy
Logged Offline
Site Site Private Message Private message Reply: 4 - 4
1 Pages 1 Recommend Thread
Print Print Thread

XBLite Forum    General Boards    XBLite Programming  ›  Single Instance Program

Thread Rating
There is currently no rating for this thread