IonicWind Software

IWBasic => The Roundtable => Topic started by: ckoehn on October 24, 2018, 10:31:29 AM

Title: Single Instance
Post by: ckoehn on October 24, 2018, 10:31:29 AM
This will allow a single instance of your program to run and will activate that instance.  So if your program is running and you try to run it again, it will bring up your first program and will not start the second one.

usage: SetInstance("Unique sMutex", "Error Message", "Caption of Error Message", "Caption of window to look for, can be partial title.  If found will activate it.")

example: SetInstance("SomethingUnique", "Program is already running", "My Program Name", "My Uni")
         If program is already running, it will display in a messagebox "Program is already running", with a title of "My Program Name".  It will then search for a caption in which the left 6
         chars are "My Uni" and if found will activate it.  If not found it won't do anything.

example: SetInstance("SomethingUnique","","","My Unique Program")
         If the program is already running, it will find it and activate it, if "My Unique Program" matches the left 17 characters of the title.  If it can't find it, it won't do anything.

example: SetInstance("SomethingUnique","Program is already running", "My Program Name")
         If program is already running, it will display in a messagebox "Program is already running", with a title of "My Program Name".  It will then search for a caption that the left 15
         chars are "My Program Name" and if found will activate it.  If not found it won't do anything.

You need to include it at the top of your program and call it immediately.

$INCLUDE "windowssdk.inc"
$INCLUDE "singleinstance.inc"

SetInstance("UniqueName", "", "Program title")


If for some reason it doesn't work, let me know.

Later,
Clint
Title: Re: Single Instance
Post by: Brian on October 24, 2018, 12:00:25 PM
I have used this code quite successfully. Place right after $include "windowssdk.inc" line:

UINT hMutexOneInstance=CreateMutexA(NULL,FALSE,"MYPROGRAM-198FA850-B10E-11D7-BD47-AA6162709673")
   UINT gle=GetLastError()
IF (gle=183) OR (gle=5)
'Program already open. Terminate immediately with message
   MESSAGEBOX 0,"This program is already running","Program error",@MB_ICONEXCLAMATION
   END
ENDIF

Brian
Title: Re: Single Instance
Post by: ckoehn on October 24, 2018, 03:49:22 PM
That was the basis I started from.  But then I wanted it to SHOW the program.  Got that working to.  Since the title had to match exactly, it wouldn't find the program that had other info in the title, so I had to make it search through the windows for partial titles matches. This is what I currently ended up with.  If your window is even minimized, it will restore it.

Later,
Clint
Title: Re: Single Instance
Post by: Brian on October 25, 2018, 03:58:14 AM
Clint,

I will set up an example of your program to try. I just tried a program that has my code in, and it does seem to work properly, even when minimised. Thanks for posting your example

Brian
Title: Re: Single Instance
Post by: ckoehn on October 26, 2018, 08:36:57 AM
Here are a few changes.  It quit working for me all of a sudden.  Must have made some change that affected it.  Seems like it was transfering a string variable through several routines.

Here is the one that seems to work.

Later,
Clint