Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


How to Prevent More than One Copy of WinBatch from Being Run

Keywords: multiple copies of WinBatch duplicate 

How to Run ONLY One Instance of a Script

To preventing the same copy of a Winbatch script from launching more than once:

Put this at the top of your existing script:


       ;if the window title already exists, then exit this script
	If WinExist("Just Once") == @TRUE Then Exit

	;Then change the window title for the program you are running to "Just Once"
	WinTitle("~WBT - ", "Just Once")    ;Change the name of the window.
	message("hello", "world")

Now, if a second copy of the winbatch script tries to run, it will check for the window name, find the window and exit.


Another way, if your script is launching another application:

Put this at the top of your existing script:

	;check to see if the Window Title has  been changed yet.  
	;First time run the name changes

	;if the window title already exists, then exit this script
	If WinExist("Just Once") == @TRUE Then Exit 

	;Then change the window title for the program you are running to "Just Once"
	Run("winword.exe", "")
	WinTitle("Microsoft Word", "Just Once")	   ;Change the name of the window.

	Message("Hi", "I'm the only copy")  

And a user suggested the following:

Usually this is done using mutexs, 2 mutexs with the same name can't exist in the computer.

Try to put something unique that identifies your script as the mutex name, and don't forget to close the mutex before you exit the script.

Try this and you'll see that you can't run 2 hinstances:

kernel32=strcat(dirwindows(1),"kernel32.dll")

ERROR_ALREADY_EXISTS=183

mutexname="*mywinbatchappnamev1.x*" 

hmutex=dllcall(kernel32,long:"CreateMutexA",lpnull,long:0,lpstr:mutexname)

if DllLastError()==ERROR_ALREADY_EXISTS ;mutex exist an hinstance is running
  message("Error","Only 1 hinstance is allowed")
  exit
endif
  
;mutex does not exist, do processing
boxopen("","Processing..")
timedelay(10)

;close mutex
dllcall(kernel32,long:"CloseHandle",long:hmutex)


Article ID:   W13276
Filename:   Prevent more than one WB Copy from Being Run.txt