Announcement

Collapse
No announcement yet.

Preventing program running more than once

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

  • Preventing program running more than once

    I have a program that I want to ensure only runs once. That is
    only one copy is running on any of the network computers at any
    one time. The program is always stored in the on only one of the
    network computers and is alway in the same directory.
    .
    What I figured I would do, at the start up of the program, is
    to open a file for exclusive access and have an error check to
    see if the file can not be opened which would indicate that the
    program is already running. If this occurs the program (second one)
    will immediately end.
    .
    This approach seams to be working to prevent a network user from
    running the program when someone else is running it.
    .
    Does anyone see any problems with this approach?

    ------------------

  • #2
    Just when/if the app crashes or hangs the file may still be locked.

    Still an alright method though. A better since it is networked
    anyway is to setup a server app they connect to and perhaps only
    one IP is allowed to run. Maybe even a time period that sends
    a test to the app to see if it is still active too.

    ------------------
    If you aim at nothing...you will hit it.
    sigpic
    Mobile Solutions
    Sys Analyst and Development

    Comment


    • #3


      Code:
      FUNCTION PBMAIN
          LOCAL lpClassName AS ASCIIZ * 255
          LOCAL hMutex AS LONG
          '
          lpClassName = "PrintFileList"
          IF App_PrevInstance(lpClassName) THEN
               MSGBOX """Print Directory"" is already running !",,"Print File List Best"
               EXIT FUNCTION
          END IF
          hMutex = CreateMutex(BYVAL %Null, 0, lpclassName)
      .
      .
      .
      .
      ------------------
      Trento Castricone
      http://www.raineyday.com
      mailto:[email protected][email protected]</A>
      Trento Castricone
      www.fileraptor.com
      [email protected]

      Comment


      • #4
        I use Trento's method, however if one user is local & another logs on via Windows Remote desktop & tries to run you software, he/she will succeed. I'm looking into an alternate approach. If I make it work I'll post code.

        Russ

        ------------------
        "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


        • #5
          EDITED: OOps - saw you were lo oking for network use.
          A Lock file is about the simplest way to go......
          Maybe have it write a PID file like Apache does, put the PID inside or put the computername in a file.....

          With the help of other coders in teh late 90's we put this together, uses a mutex.

          Code:
          Declare Function App_PrevInstance(lpclassName As Asciiz) As Long
          '
          '
          
          Function PBmain() 
          Local hMutex as Dword 'Or long works
          szClassName = "MYAPP"
          g_szMine is my appname
          g_szCCS is my company name
          If IsTrue App_PrevInstance(szClassName) Then
              MessageBox ByVal 0, "Another instance of " + g_szMine + " is already running!", ByVal StrPtr(g_szMine), ByVal %MB_ICONSTOP
              Exit Function
          Else
              hMutex = CreateMutex(ByVal %Null, 0, szClassName)
          End If
          CloseHandle hMutex
          End function
          
          
          '
          '
          '
          Function App_PrevInstance(lpclassName As Asciiz) As Long
          Local hMutex As Long
          hMutex = CreateMutex(ByVal %Null, 0, lpclassName)
          If hMutex = 0 Then Exit Function ' Error in Mutex
          If GetLastError = %ERROR_ALREADY_EXISTS Or FindWindow(lpClassName, "") > 0 Then Function = %TRUE
          CloseHandle hMutex
          End Function
          ------------------
          Scott Turchin
          MCSE, MCP+I
          Computer Creations Software http://www.tngbbs.com/ccs

          [This message has been edited by Scott Turchin (edited September 23, 2005).]
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment

          Working...
          X