April 26, 2024, 08:09:59 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Single Instance

Started by ckoehn, October 24, 2018, 10:31:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ckoehn

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

Brian

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

ckoehn

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

Brian

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

ckoehn

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