Hi all
I was wondering how I can stop multiple executions of my program?
------------------
Henning
I was wondering how I can stop multiple executions of my program?
------------------
Henning
DIM szTemp AS ASCIIZ*%MAX_PATH szTemp = "myprogram.exe" ' DIM hMutex AS LONG ' hMutex = CreateMutex(BYVAL %NULL, 0, szTemp) IF hMutex = %NULL THEN EXIT FUNCTION IF GetLastError = %ERROR_ALREADY_EXISTS THEN EXIT FUNCTION
#Compile Exe #Dim All #Register None #Include "WIN32API.INC" %Sign1 = &H12345678& ' Any unique %Sign2 = &H90123456& Function EnumWindowsProc(ByVal hWnd As Long, Param As Long Ptr) As Long If GetProp (hWnd, ByVal 1) = %Sign1 Then _ If GetProp (hWnd, ByVal 2) = %Sign2 Then @Param = hWnd: Exit Function Function = 1 End Function CallBack Function DlgProc Select Case CbMsg Case %WM_INITDIALOG SetProp CbHndl, ByVal 1, ByVal %Sign1 SetProp CbHndl, ByVal 2, ByVal %Sign2 End Select End Function Function PbMain Dim hDlg As Long, k As Long EnumWindows CodePtr(EnumWindowsProc), VarPtr(k) If k <> 0 Then ShowWindow k, %SW_RESTORE: SetForegroundWindow k: Exit Function ' dirty and incorrect method; simply for test Dialog New %HWND_DESKTOP, "Started at " + Time$, 240, 120, 150, 60, %WS_CAPTION Or %WS_OVERLAPPEDWINDOW To hDlg Dialog Show Modal hDlg, Call DlgProc End Function
#Dim All #Register None #Option Version4 Type SemaphoreType AppAlreadyRunning As Long LastShutdownSuccessful As Long End Type ' Put in Registry or cfg/ini file $Hash = "0102030405060708090A0B0C0D0E0F101112131415161718191A" $SemaPath = "SemaPhore\" Declare Function AppRunning( HashName As String ) As Long Declare Function ReHash( HashHex As String ) As String Declare Sub CreateDirectoryTree( ByVal NewPath As String ) Declare Sub DoStuff() Declare Sub UpdateSemaphore( ReHashName As String ) Global CurDrive$ Function PbMain Local lResult As Long CurDrive$ = Left$(CurDir$,3) 'Create the Path for the SemaPhore file CreateDirectoryTree CurDrive$ & $SemaPath lResult = AppRunning(ReHash($Hash)) If (lResult > 0& ) Then Beep Select Case lResult Case 1& StdOut "No can Do. App Running..." Case 2& StdOut "No can Do. Semaphore File already opened." End Select Exit Function End If DoStuff Call UpdateSemaphore( ReHash($Hash) ) End Function Sub DoStuff WaitKey$ End Sub Function AppRunning( ReHashName As String ) As Long Local DaError As Long Local udtSema As SemaphoreType ErrClear Open CurDrive$ & $SemaPath & ReHashName For Binary Access Read Write Lock Read Write As #1 DaError = ErrClear If DaError > 0& Then StdOut "Error Opening Semaphore File:" & Str$(DaError) Function = 2& End If Get #1,, udtSema Select Case udtSema.AppAlreadyRunning Case 0& udtSema.AppAlreadyRunning = 1& udtSema.LastShutdownSuccessful = 0& Put #1,1, udtSema Function = 0& Case 1& If udtSema.LastShutdownSuccessful = 0& Then Function = 1& Else Function = 0& End If End Select Close 1 End Function Sub UpdateSemaphore( ReHashName As String ) Local DaError As Long Local udtSema As SemaphoreType udtSema.LastShutdownSuccessful = 1& udtSema.AppAlreadyRunning = 0& ErrClear Open CurDrive$ & $SemaPath & ReHashName For Binary Access Write Lock Read Write As #1 DaError = ErrClear If DaError > 0& Then StdOut "Error Opening Semaphore File:" & Str$(DaError) & "! Investigate and manually update file." Exit Sub End If Put #1,, udtSema Close 1 End Sub Function ReHash( HashHex As String ) As String ' Do something real here with hash code... Rehash = "HashTest" End Function Sub CreateDirectoryTree( ByVal NewPath As String ) Local s As String Local Pth As String Local l As Long If Right$(NewPath,1) = "\" Then NewPath = Left$(NewPath, Len(NewPath) -1 ) End If l = 1 Do s = Parse$( NewPath, "\", l ) If Len(s) > 0& Then Pth = Pth & s & "\" MkDir Pth Incr l Else Exit Do End If Loop End Sub
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment