Loading ...
Sorry, an error occurred while loading the content.

Disallowing multiple instances of a program

Expand Messages
  • KR8ZZY
    Hi. It s the pest again... The program I ve been working on runs in the background (no visible window). This has caused me on occasion to double click on its
    Message 1 of 4 , Mar 2, 2011
    View Source
    • 0 Attachment
      Hi. It's the pest again...

      The program I've been working on runs in the background (no visible window). This has caused me on occasion to double click on its icon and start a second instance of the program.

      I've written some code into the program that helps prevent this from happening:

      This part runs at startup:


      'Check for another instance running
      [non.concurrency.check]
      progloc$ = "non.concurrency.check"

      if firstrun = 0 goto [user.default.profile]
      x = 0
      killfile$ = ""

      files fileloc$, "\Time Minder.run", info$()
      if val(info$(0,0)) = 0 goto [user.default.profile]
      killfile$ = fileloc$ + "\Time Minder.run"
      if concurrency = 1 then
      notice "TIME MINDER: ERROR" + chr$(13)+"Another instance of Time Minder is already running."
      stop
      else
      kill killfile$
      end if

      oldtime$ = ""
      'Wait 2 seconds
      [non.concurrency.check.wait]
      progloc$ = "non.concurrency.check.wait"

      if oldtime$ <> time$() then
      oldtime$ = time$()
      x = x + 1
      if x = 2 then
      concurrency = 1
      goto [non.concurrency.check]
      end if
      end if
      goto [non.concurrency.check.wait]

      And this part runs during the normal cycling of the program:


      files fileloc$, "\Time Minder.run", info$()

      if val(info$(0,0)) = 1 goto [looper.1]
      writefile$ = fileloc$ + "\Time Minder.run"
      open writefile$ for output as #concurrent
      print #concurrent, date$()
      print #concurrent, time$()
      close #concurrent

      goto [looper.1]

      The problem I have is that since I have the .exe on my hard drive and on my thumb drive, if I start the one from my hard drive, and then start the one from my thumb drive (or start the one from within LB itself), the new instance does not detect the other.

      I'd rather do this without writing to a file, but rather somehow check if the process is running (like looking for it in the task manager process list).

      Is there a way to retrieve the contents of the task manager processes list and search for a process that is running?

      Thanks again.
      Paul
    • JanetTerra
      Try Ken Lewis Preventing More Than One Instance of Your Program will help. You may want to test this code out in Vista and 7, since it was written a few
      Message 2 of 4 , Mar 2, 2011
      View Source
      • 0 Attachment
        Try Ken Lewis' "Preventing More Than One Instance of Your Program" will help. You may want to test this code out in Vista and 7, since it was written a few years ago.

        http://lbpe.wikispaces.com/newsletter.2002#x--Issue%20#102%20-%20Nov.%202002



        --- In libertybasic@yahoogroups.com, "KR8ZZY" <plvinoski@...> wrote:
        >
        > Hi. It's the pest again...
        >
        > The program I've been working on runs in the background (no visible window). This has caused me on occasion to double click on its icon and start a second instance of the program.
        >
        > I've written some code into the program that helps prevent this from happening:
        >
        > This part runs at startup:
        >
        >
        > 'Check for another instance running
        > [non.concurrency.check]
        > progloc$ = "non.concurrency.check"
        >
        > if firstrun = 0 goto [user.default.profile]
        > x = 0
        > killfile$ = ""
        >
        > files fileloc$, "\Time Minder.run", info$()
        > if val(info$(0,0)) = 0 goto [user.default.profile]
        > killfile$ = fileloc$ + "\Time Minder.run"
        > if concurrency = 1 then
        > notice "TIME MINDER: ERROR" + chr$(13)+"Another instance of Time Minder is already running."
        > stop
        > else
        > kill killfile$
        > end if
        >
        > oldtime$ = ""
        > 'Wait 2 seconds
        > [non.concurrency.check.wait]
        > progloc$ = "non.concurrency.check.wait"
        >
        > if oldtime$ <> time$() then
        > oldtime$ = time$()
        > x = x + 1
        > if x = 2 then
        > concurrency = 1
        > goto [non.concurrency.check]
        > end if
        > end if
        > goto [non.concurrency.check.wait]
        >
        > And this part runs during the normal cycling of the program:
        >
        >
        > files fileloc$, "\Time Minder.run", info$()
        >
        > if val(info$(0,0)) = 1 goto [looper.1]
        > writefile$ = fileloc$ + "\Time Minder.run"
        > open writefile$ for output as #concurrent
        > print #concurrent, date$()
        > print #concurrent, time$()
        > close #concurrent
        >
        > goto [looper.1]
        >
        > The problem I have is that since I have the .exe on my hard drive and on my thumb drive, if I start the one from my hard drive, and then start the one from my thumb drive (or start the one from within LB itself), the new instance does not detect the other.
        >
        > I'd rather do this without writing to a file, but rather somehow check if the process is running (like looking for it in the task manager process list).
        >
        > Is there a way to retrieve the contents of the task manager processes list and search for a process that is running?
        >
        > Thanks again.
        > Paul
        >
      • Stefan Pendl
        ... Search this group online for Mutex and you will find a post by Richard, which is using the recommended way, since the newsletter article is missing some
        Message 3 of 4 , Mar 3, 2011
        View Source
        • 0 Attachment
          >
          > Try Ken Lewis' "Preventing More Than One Instance of Your
          > Program" will help. You may want to test this code out in
          > Vista and 7, since it was written a few years ago.
          >
          > http://lbpe.wikispaces.com/newsletter.2002#x--Issue%20#102%20-
          > %20Nov.%202002
          >

          Search this group online for "Mutex" and you will find a post by Richard, which is using the recommended way, since the newsletter
          article is missing some important parts.

          ---
          Stefan Pendl
          http://stefanpendl.runbasichosting.com/

          Liberty BASIC 4.04 Pro ... http://www.libertybasic.com/assist.html
          Liberty BASIC 4.04 ....... http://www.libertybasic.com/lb404setup.exe

          Liberty BASIC Bug Tracker ... http://libertybasicbugs.wikispaces.com/

          Books at http://www.lulu.com/ and http://www.amazon.com/
          Alyce Watson ... APIs for Liberty BASIC
          Carl Gundel .... Beginning Programming with Liberty BASIC

          Windows 7 Home Premium 64-bit RTM
          AMD Turion X2 RM-70 2GHz, 4GB RAM
        • Stefan Pendl
          ... Search this group online for Mutex and you will find a post by Richard, which is using the recommended way, since the newsletter article is missing some
          Message 4 of 4 , Mar 3, 2011
          View Source
          • 0 Attachment
            >
            > Try Ken Lewis' "Preventing More Than One Instance of Your
            > Program" will help. You may want to test this code out in
            > Vista and 7, since it was written a few years ago.
            >
            > http://lbpe.wikispaces.com/newsletter.2002#x--Issue%20#102%20-
            > %20Nov.%202002
            >

            Search this group online for "Mutex" and you will find a post by Richard, which is using the recommended way, since the newsletter
            article is missing some important parts.

            ---
            Stefan Pendl
            http://stefanpendl.runbasichosting.com/

            Liberty BASIC 4.04 Pro ... http://www.libertybasic.com/assist.html
            Liberty BASIC 4.04 ....... http://www.libertybasic.com/lb404setup.exe

            Liberty BASIC Bug Tracker ... http://libertybasicbugs.wikispaces.com/

            Books at http://www.lulu.com/ and http://www.amazon.com/
            Alyce Watson ... APIs for Liberty BASIC
            Carl Gundel .... Beginning Programming with Liberty BASIC

            Windows 7 Home Premium 64-bit RTM
            AMD Turion X2 RM-70 2GHz, 4GB RAM
          Your message has been successfully submitted and would be delivered to recipients shortly.