Announcement

Collapse
No announcement yet.

Detecting if an app is already started

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

  • Detecting if an app is already started

    Hi all,
    Can anyone tell us a way for an app to detect (when it's first launched) that another instance of it is already running, and if it is, to terminate itself?

    For example, if "MyPBApp.exe" is already running and the user doubleclicks the icon again, have the second launch of it detect this and exit, maybe with a messagebox reminding the user that doing this is a no-no.

    I'm using PBWin 8.

    Thanks in advance to anyone that can help with this one.

  • #2
    Check out my MP3 player in the source forum:

    PowerBASIC and related source code. Please do not post questions or discussions, just source code.


    Specifically, the Is_Already_Running function. Does exactly what you want.

    I didn't write that particular function. Got it off the forum somewhere and I can't remember the original author. Sorry.
    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


    • #3
      Mel, I don't know if something happened to cut off part of the source code you referenced, or not, but I didn't see the Is_Already_Running function. So I don't know if it is using this method or not.

      But a common method to do this is to create a mutex which is a system wide placeholder that your program can use to know if it is already running.

      Here is a simple example:

      Code:
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "WIN32API.INC"
      
      Function PBMain()
         Local zName    As Asciiz * %MAX_PATH
      
         zName = "MyUniqueNameForThisProgram"
         If CreateMutex(ByVal 0, 1, zName) Then
            If GetLastError() = %ERROR_ALREADY_EXISTS Then
               MsgBox "already started... aborting",,"Already started"
            Else  
               'not already running so continue processing
               ShowDIALOG1 %HWND_DESKTOP
            End If
         Else
            MsgBox "CreateMutex failed",,zName
         End If
      END FUNCTION
      [Added later: I see that function now. Looks like some formatting issues when you pasted it because the function line is remarked out]
      "I haven't lost my mind... its backed up on tape... I think??" :D

      Comment


      • #4
        Originally posted by Chris Burgess View Post
        Hi all,
        Can anyone tell us a way for an app to detect (when it's first launched) that another instance of it is already running, and if it is, to terminate itself?

        For example, if "MyPBApp.exe" is already running and the user doubleclicks the icon again, have the second launch of it detect this and exit, maybe with a messagebox reminding the user that doing this is a no-no.

        I'm using PBWin 8.

        Thanks in advance to anyone that can help with this one.
        Do a search for "MUTEX" which is used to check if an app is already running. There are quite a few examples in the forums. Not at all difficult to use.

        ===========================
        "First they ignore you,
        then they laugh at you,
        then they fight you,
        then you win."
        Mahatma Gandhi (1869-1948)
        ===========================
        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


        • #5
          another instance of it is already running
          Note: It could still be running on another station.
          If that is a concern, open a common file.

          There are many links on this subject.

          PowerBASIC and related source code. Please do not post questions or discussions, just source code.

          User to user discussions about the PB/Win (formerly PB/DLL) product line. Discussion topics include PowerBASIC Forms, PowerGEN and PowerTree for Windows.


          http://www.powerbasic.com/support/pb...ad.php?t=22702 *
          Last edited by Mike Doty; 1 May 2009, 03:35 PM. Reason: * My solution by recording logons
          The world is full of apathy, but who cares?

          Comment


          • #6
            Originally posted by William Burns View Post
            ...didn't see the Is_Already_Running function...

            I see that function now....
            The source got scrambled during the transition from the old board to the new. Stuff happens you know.

            As far as I can tell, the function itself is intact and should work.
            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


            • #7
              As Mike said could still be running on a network station, nor would the simpe mutex described detect if it is running in another terminal services or Citrix session

              Comment


              • #8
                If you need to find out who is running or how many instances of you program are running on the system, see my post in the following thread:

                User to user discussions about the PB/Win (formerly PB/DLL) product line. Discussion topics include PowerBASIC Forms, PowerGEN and PowerTree for Windows.


                The above code will enumerate all processes, simply a case of comparing the process executable using a FindFirstFile() comparison. I use similar code to enumerate all running instances and shut the others down if an auto-update is imminent (the user is prompted about this first). It won't be able to detect others running the app from another computer (ie. on a network share), so I recommend the user against that.

                PS. Weirdly, the forum search here turned up nothing for "CreateToolhelp32Snapshot" (all posts, all forums), but I found the title in POFFS and used that.
                kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

                Comment


                • #9
                  Kev
                  Interesting approach, current version requires 2000 server or 2000 Pro or above. Without diverting the thread, those who try to protect their code with packers etc might want to look at this function in the same toolbox Toolhelp32ReadProcessMemory Function.
                  John

                  Comment

                  Working...
                  X