Announcement

Collapse
No announcement yet.

Ensuring only one instance of a program – how?

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

  • Ensuring only one instance of a program – how?

    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.

    Best regards,

    Erik

  • #2
    Multiple examples here.

    Search 'mutex' and/or 'instance' in message subject.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      ?

      Eric,

      Is there a simple method by which a program can ensure that there at any time is only one instance of another program running?
      Do you want to ensure, there is only one instance of your own program
      running at a given time, or there is only one instance of a third
      party program running at a given time.

      What do you mean by ensure?
      - just look to make sure (count instances)
      - do something to make sure (kill a second (third and so on ..) instance)


      Ensuring your own program runs only in a single instance is very easy.
      You need some kind of a global flag (a file, a .ini file, a memory mapped
      file, a mutex, a registry entry, ...). When your program starts, it must
      check, if this flag is set. If so - it must abort (because there is already
      another instance running), if not - it must set this flag by itself to prevent
      other instances from running. When exiting don´t forget to reset this flag
      again.


      Hope this helps


      guenther

      Comment


      • #4
        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).

        I think the technique you describe Guenther is fine. I will try this. Thank you.
        Last edited by Erik Christensen; 29 Nov 2008, 08:49 AM.

        Comment


        • #5
          Check my MP3 player at:

          http://www.powerbasic.com/support/pb...ad.php?t=25095

          and look for the Is_Already_Running function
          There are no atheists in a fox hole or the morning of a math test.
          If my flag offends you, I'll help you pack.

          Comment


          • #6
            Thanks.

            I found a similar function in this thread:
            http://www.powerbasic.com/support/pb...ad.php?t=25148

            This prevents more than one instance, which is fine.

            If for some reason there are already more instances of a program, is it then possible to delete (by another program) the superfluous ones?
            Last edited by Erik Christensen; 29 Nov 2008, 10:58 AM.

            Comment


            • #7
              I just found this link:

              http://www.powerbasic.com/support/pb...ad.php?t=24019

              I think this will answer my question.

              Comment


              • #8
                Eric
                Just a reminder this is not as simple as it sounds if you are in a situation with multiple name spaces such as when running with Terminal Services or Citrix or even with XP or Vista with fast user switching as unless exceptionally careful many of the normally checked objects will only be visible in a single namespace.
                Some discussion here http://www.powerbasic.com/support/pb...ad.php?t=37884
                John

                Comment


                • #9
                  I managed to produce this code based on previous contributions in the PowerBasic Forum. Thanks a lot.
                  http://www.powerbasic.com/support/pb...d.php?p=303356

                  Comment


                  • #10
                    >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
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      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
                      It's a pretty day. I hope you enjoy it.

                      Gösta

                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                      Comment


                      • #12
                        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
                        Client Writeup for the CPA

                        buffs.proboards2.com

                        Links Page

                        Comment


                        • #13
                          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
                          "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers

                          Comment


                          • #14
                            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.
                            http://www.powerbasic.com/support/pb...d.php?p=303395
                            It may come as a surprise to you to see how many there are.

                            Best regards

                            Erik

                            Comment


                            • #15
                              >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?
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                I am talking about other programs, not your own.

                                Comment


                                • #17
                                  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.

                                  Comment


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

                                    Comment


                                    • #19
                                      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.

                                      Comment


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

                                        Comment

                                        Working...
                                        X