Announcement

Collapse
No announcement yet.

Ensuring only one instance of a program – how?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    You are assuming that this type of code will be used only to determine if your own application is already running.
    Actually, I was assuming it would be used to determine if an instance of THIS program was running... which was, after all, the original question.

    MCM

    Leave a comment:


  • David Roberts
    replied
    Re your version 2 Erik

    ' This version will is based on the file size. It will kill instances with the
    ' same file size even if the file has been renamed by someone.
    .....
    .....
    .....
    IF DirDataVar.FileSizeLow = FileSize AND lngR <> kk THEN ' kill other files of same size even if renamed.

    Before killing I'd be inclined to make sure that they have the same SHA1 hash value.
    Last edited by David Roberts; 7 Dec 2008, 02:46 PM. Reason: Spelling

    Leave a comment:


  • Graham McPhee
    replied
    >There can't be. You prevented this from happening.

    You are assuming that this type of code will be used only to determine if your own application is already running.

    The scope of the code I wrote was to cover detection of multiple instances of selected applications, SQL Query analyser for example, that I had no control over the launching of; but might still cause my application problems when it is trying to apply tsql scripts to one or more databases.

    Leave a comment:


  • Mike Doty
    replied
    Synonyms
    charisma, dash, flare, style, swagger, élan, flair, flourish, verve, vigor,

    Leave a comment:


  • Michael Mattias
    replied
    The file solution is awfully straightforward.... but if you don't need the panache points......

    Leave a comment:


  • Mike Doty
    replied
    To make it more useful, the locked file can contain the computer name and time the file was locked.
    Makes it much easier on a network to see who is holding up production.

    Leave a comment:


  • Erik Christensen
    replied
    Mike, your suggestion was fine.

    Leave a comment:


  • Michael Mattias
    replied
    >What was wrong with my suggestion

    Nothing. Testing a file either for 'existence' or 'in use' is a perfectly valid way to check if a program is running.

    MCM

    Leave a comment:


  • Mike Doty
    replied
    What was wrong with my suggestion

    Leave a comment:


  • Erik Christensen
    replied
    I apologize for having been somewhat unclear along the way. Thanks John for the wise comments and suggestions. I have really learned something.

    Leave a comment:


  • John Petty
    replied
    I really don’t understand why there is so much code here that is not totally reliable for such a simple problem. Mutexes only work if you know how to create them as Global (no examples shown here for that) and then only on the local computer and the default namespace has yet again changed in 2008, same applies for matching window names, as for module checking as Mike says they only have to rename the program and of course none of the methods work if the program is being run by workstations sharing the data.
    As an example on a 2003 server if a user logs on locally as say administrator and leaves a program running, then you log on as administrator using remote desktop the running program will not show even in task manager, you are in a different namespace.
    The solution is incredibly simple, never allow two copies of the program to start.
    In WINMAIN
    ON ERROR RESUME NEXT
    OPEN “c:\ not allowed.wtf” for binary LOCK READ WRITE as #1
    If ERR = 70 EXIT FUNCTION
    You can be nice if you like and give a message box.
    Worried about network users then
    OPEN \\shared drive\ etc
    How easy can it get.

    Leave a comment:


  • Mike Doty
    replied
    Should also mention somebody could rename program and run it.
    Last edited by Mike Doty; 30 Nov 2008, 11:02 AM.

    Leave a comment:


  • Mike Doty
    replied
    Prevent anyone on a network. "wait.dat" could be on the internet if TCP open is used.
    Code:
    FUNCTION PBMAIN () AS LONG
      OPEN "wait.dat" FOR BINARY AS #9999
      IF ERR THEN ? "Sorry, in use":EXIT FUNCTION
      ? "I'm running"
    END FUNCTION
    Post #1
    Is there a simple method by which a program can ensure that there at any time is only one instance of "another program running?" Thanks in advance.

    Post #4 says "my own program"

    I want to do something to make sure that only one instance of my own program exists at any time (i.e. kill a second (third and so on ..) instance).
    Last edited by Mike Doty; 30 Nov 2008, 10:55 AM.

    Leave a comment:


  • Erik Christensen
    replied
    I am talking about other programs, not your own.

    Leave a comment:


  • Michael Mattias
    replied
    >but if for some reason there are already more instances of a program

    ???

    There can't be. You prevented this from happening.

    ???

    You can't create program code on the assumption you have made an error. Kind of self-defeating, no?

    Leave a comment:


  • Erik Christensen
    replied
    Many Thanks. The mutex method is fine, but if for some reason there are already more instances of a program, the method provided in the code may be better.
    The routines by Graham McPhee can deal with all processes in the computer.

    I actually used his routines for another code which lists all processes in the computer.

    It may come as a surprise to you to see how many there are.

    Best regards

    Erik

    Leave a comment:


  • Russ Srole
    replied
    Stick this in your WinMain function
    Code:
        Local PrgName As Asciiz * 255
        Local hInstanceMutex&
        PrgName = "My_Program_Name" ' Important
        hInstanceMutex = CreateMutex(ByVal %Null, 0, PrgName)
        If hInstanceMutex = 0 Then Exit Function ' Error in Mutex
        If GetLastError = %ERROR_ALREADY_EXISTS Then
            MsgBox  "This old Program is already running"
            Exit Function
        End If

    Leave a comment:


  • Fred Buffington
    replied
    Here's what I use that is probably in one of the links provided in earlier posts
    Code:
        CASE %WM_INITDIALOG
            gWhatApp = lTitle$ ' title to find 
            EnumWindows CODEPTR(EnumWindowsProc), 0
           IF gAppCount>1 THEN
             MSGBOX "Warning: "+FORMAT$(gAppCount) & " occurrence(s) of Client Writeup running"
           END IF

    Leave a comment:


  • Gösta H. Lovgren-2
    replied
    Erik, here's some simpler code. I got most of it from the Forums a long time ago and don't remember who should get credit.
    Code:
            Case %WM_INITDIALOG '<- for processing that needs to occur when the program loads 
                'Check to see if program already running
                   Local hmutex&, smutex$ 'just used for testing program existence
                smutex$ = " Checkbook"   'Program name 
                hmutex& = CreateMutex(ByVal %Null, 0, ByVal StrPtr(smutex$))'check if running
                If hmutex& <> %Null Then 'Program probably already running
                   If GetLastError() = %error_already_exists Then 'Constant defined in WinAPI
                      MsgBox SmuTex$ & " is already running", %Top, Using$("Error code #",Err)
                     Dialog Post CbHndl, %Dialog_End, 0, 0 'Tell program not to run
                                                           'Can't end it in %WM_InitDialog
                                                           'Tell it to end in %WM_Command
                     Exit Function 
                  End If
                End If
                'Continue Any initializing code here

    Leave a comment:


  • Michael Mattias
    replied
    >I managed to produce this code ...

    Kind of the 'long way,' isn't it?
    Code:
    $MUTEX_NAME = "whatever" 
    
    FUNCTION WinMain(...) 
    
      SzMutex = $MUTEX_NAME 
      hMutex =  OpenMutex (....szMutex  no_ownership_needed) 
      IF ISTRUE Mutex THEN 
          'program already running , return error code and exit
          CloseHandle hMutex
          FUNCTION = 1 
          EXIT FUNCTION
      ELSE
         hMutex = CreateMutex (..) 
         ' next program instance will be able to open the mutex forcing it to exit
      END IF
    
      ' here there is a debate... you can CloseHandle (hMutex), or you can let 
      ' windows do it for you when your process completely ends. 
      '
    
    END FUNCTION

    Leave a comment:

Working...
X
😀
🥰
🤢
😎
😡
👍
👎