Announcement

Collapse
No announcement yet.

listing of my program to stop a second instance of a program from running

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

  • listing of my program to stop a second instance of a program from running


    a program to keep a second occurrance of a program for running in windows or msdos.

    this program would not have been done had some people
    not mentioned about locking files as an alternative to
    keeping up with what programs are running.
    i wish to thank them for reading my questions and answering them.
    on a previous thread posted by myself on the subject to try to keep
    a program from running twice at the same time under windows.
    it might even work under linux, who knows.


    i wanted to write a tsr under powerbasic but to keep the tagfile open
    was not possible, so i had to use shell command inside powerbasic.
    also by using the shell command, rather than a tsr written in powerbasic, i believe
    the main program PROGDBNO does not use much processing power. i really did not test
    it much to find out on the power thing but did a little.

    ok people here is what i have done
    maybe somebody else will clean it up
    it is working right now for me
    feel free to use this program in anyway you want
    except sell it
    i am in high hopes some of you will make great changes to it and share the code
    i believe by editing out a lot of the text will make it smaller in memory
    during the shell operation.
    i am into fast and efficient.
    but this is about the only 4th program i have ever wrote using powerbasic
    if you can make the program smaller memory please do
    and report back.
    i wonder what ultashell wound do to this program
    i do not have time to write the program in pb/cc
    maybe somebody will convert the code over to it for use.

    i used only the code that came with powerbasic 3.5 and no third party code

    the only thing i feel i left out was to erase the tagfile in this program upon
    exiting back to dos after the shelled program returned, and only erase the tagfile
    if this program created the tagfile.
    if the tagfile already existed when this program was started, we want to leave it there
    when we exit, because we do not know what the tagfile was already being used for.

    so the only problem i know of in this program is that if your system crashes,
    it could damage or erase the tagfile and that will only matter if some other program is
    using the tagfile for some reason. that is why i think a directory called "\instance" is a good one
    and use a tagfile that ends in ".tag" and put it in the "\instance" directory,
    example of my tagfile "c:\instance\prog0001.tag".

    also being that this program was designed to be run inside a batch file from the getgo.
    you can run multiple instances of the same program, just use a different batch file and
    a different tagfile.



    ------------------------------------------------------
    Code:
       rem progdbno.bas
       rem this program is intended mostly to work under windows
       rem
       rem the reason for this program
       rem is to keep a second instance(occurance) of the same program from running
       rem
       rem this program shells out to second program if it does not
       rem find a file that is being used(locked) as a tagfile
       rem the file being held in the locked position is a so called switch
       rem if the file is being used, it indicates a no go on the shelled(second) program
       rem the command tail after progdbno has the tag file and the shell command
       rem on the command tail supply	/t then the *** file  and /s for the shell command
       rem
       rem powerbasic source code
       rem version 06-09-2005
       rem compiled with powerbasic 3.5 for dos
    
    rem $cpu 80386 'program works on any cpu
    $OPTIMIZE SPEED 'make smallest possible executable
    $DEBUG MAP OFF 'turn off map file generation
    $DEBUG PBDEBUG OFF 'don't include pbdebug support in our executable
    $LIB COM OFF 'turn off PowerBASIC's communications library.
    $LIB CGA OFF 'turn off PowerBASIC's CGA graphics library.
    $LIB EGA OFF 'turn off PowerBASIC's EGA graphics library.
    $LIB LPT OFF 'turn off PowerBASIC's printer support library.
    $LIB IPRINT OFF 'turn off PowerBASIC's interpreted print library.
    $LIB FULLFLOAT OFF 'turn off PowerBASIC's floating point support.
    $ERROR NUMERIC OFF 'turn off numeric checking
    $ERROR OVERFLOW OFF 'turn off overflow checking
    $ERROR STACK OFF 'turn off stack checking
    $COM 0 'set communications buffer to nothing
    $STRING 1 'set largest string size at 8k
    $STACK 4096 'let's use a 2k stack
    $SOUND 1 'smallest music buffer possible
    $DYNAMIC 'all arrays will be dynamic by default
    $OPTION CNTLBREAK OFF 'don't allow Ctrl-Break to exit program
    $COMPILE EXE 'this tells PB to make a standalone EXE
    $EVENT OFF
    
    rem setting up some variables used to reduce variable fragmentation
    I% = 0%
    J% = 0%
    CREATETAGFILE% = 0%
    TAGFILE$ = SPACE$( 128 )
    SHELLCOMMAND$ = SPACE$( 128 )
    
    rem reading the command tail and assigning variables to
    rem tagfile$ and shellcommand$
    
    TEMP$ = UCASE$( COMMAND$ ) + "/"
    
    I% = INSTR( TEMP$, "/T" )
    IF I% = 0% THEN GOTO COMMANDTAILBAD
    FOR J% = I% + 2% TO LEN( TEMP$ )
      IF MID$( TEMP$, J%, 1% ) = "/" THEN EXIT FOR
    NEXT J%
    TAGFILE$ = TRIM$( MID$( TEMP$, I% + 2%,( J% - I% - 2% )))
    IF TAGFILE$ = "" THEN GOTO COMMANDTAILBAD
    
    I% = INSTR( TEMP$, "/S" )
    IF I% = 0% THEN GOTO COMMANDTAILBAD
    FOR J% = I% + 2% TO LEN( TEMP$ )
      IF MID$( TEMP$, J%, 1% ) = "/" THEN EXIT FOR
    NEXT J%
    SHELLCOMMAND$ = TRIM$( MID$( TEMP$, I% + 2%,( J% - I% - 2% )))
    IF SHELLCOMMAND$ = "" THEN GOTO COMMANDTAILBAD
    
    rem checking for existing tagfile
    TEMP$ = DIR$( TAGFILE$ )
    IF LEN( TEMP$ ) = 0 THEN
      OPEN TAGFILE$ FOR OUTPUT AS #1
      PRINT #1, "this is a tag file for PROGDBNO.EXE"
      PRINT #1, "PROGDBNO.EXE is used for monitoring program instance execution"
      PRINT #1, "in other words to keep a program running more than once at the same time"
      CLOSE 1
      CREATETAGFILE% = 1%
    END IF
    
    rem trying to file lock on the tagfile
    ON ERROR GOTO TAGFILEINUSE
    OPEN TAGFILE$ FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #1
    ON ERROR GOTO 0
    
    rem shelling to the program in the command tail
    SHELL SHELLCOMMAND$
    
    rem untagging the tagfile
    rem unlock the tagfile by closing the file
    rem then erasing the file if this program created it otherwise
    rem if the tagfile existed prior to running this program do nothing with it
    CLOSE 1
    IF CREATETAGFILE% = 1% THEN KILL TAGFILE$
    
    rem normal exit of this program after shelling to a third party program
    rem has been run and returned or failure to run the shelled program
    PRINT "dos exit code set at 0"
    END 0
    
    rem file lock on to tagfile failed
    rem aborting program with an error code of 1
    TAGFILEINUSE:
    PRINT "your program was refused to run because the tag file is in use"
    PRINT "your tag file      = " + TAGFILE$
    PRINT "your shell command = " + SHELLCOMMAND$
    PRINT "dos exit code set at 1"
    END 1
    
    rem reporting of what seemed to be a bad command tail
    COMMANDTAILBAD:
    PRINT "PROGDBNO should have two command tail directives"
    PRINT "/t is used for the file to lock while your shelled program is running"
    PRINT "/s is the command with any command tail of any program you want to run"
    PRINT "ex: PROGDBNO /tc:\instance\prog0001.tag  /sc:\msdos\edit filename.txt"
    PRINT "dos exit code set at 2"
    END 2



    [This message has been edited by paul d purvis (edited June 09, 2005).]
    p purvis

  • #2

    here is the pbcc 3.05 version

    i am surely not suggesting there are no bugs here in this program
    but it seems ok to run, ha ha.



    -------------------------------------------------------
    Code:
    REM PROGDBNOW.BAS
    REM THE REASON FOR THIS PROGRAM
    REM IS TO KEEP A SECOND INSTANCE(OCCURANCE) OF THE SAME PROGRAM FROM RUNNING IN WINDOWS 
    REM TESTED WINDOWS 2000 PROFESSIONAL
    REM
    REM THIS PROGRAM SHELLS OUT TO SECOND PROGRAM IF IT DOES NOT
    REM FIND A FILE IS BEING USED(LOCKED)
    REM THE FILE BEING HELD IN THE LOCKED POSITION IS A SO CALLED SWITCH
    REM IF THE FILE IS BEING USED, IT INDICATES A NO GO ON THE SHELLED(SECOND) PROGRAM
    REM THE COMMAND TAIL AFTER PROGDBNO HAS THE TAG FILE AND THE SHELL COMMAND
    REM ON THE COMMAND TAIL SUPPLY /T THEN THE *** FILE AND /S FOR THE SHELL COMMAND
    REM
    REM VERSION 06-05-2005
    REM COMPILED WITH POWERBASIC PBCC 3.05 FOR WINDOWS
    
    REM PS I KNOW THE PROGRAM IS CRAPPY BUT IS MY SECOND PROGRAM WRITTEN FOR WINDOWS
    
    #COMPILE EXE
    
    DECLARE SUB RUNPROGRAM
    
    FUNCTION PBMAIN ()
    CALL RUNPROGRAM
    FUNCTION=EXITCODE%
    
    END FUNCTION
    
    
    SUB  RUNPROGRAM
    
    TEMP$=UCASE$(COMMAND$)+"/"
    
    I%=INSTR( TEMP$, "/T" )
    IF I%=0 THEN GOTO COMMANDTAILBAD
    
    TAGFILE$=SPACE$(128)
    FOR J%=I%+2% TO LEN(TEMP$)
    IF MID$(TEMP$,J%,1%)="/" THEN EXIT FOR
    NEXT J%
    TAGFILE$=MID$(TEMP$,I%+2%,(J%-I%-2%))
    TAGFILE$=TRIM$(TAGFILE$)
    
    IF TAGFILE$="" THEN GOTO COMMANDTAILBAD
    
    I%=INSTR( TEMP$, "/S" )
    IF I%=0 THEN GOTO COMMANDTAILBAD
    SHELLCOMMAND$=SPACE$(128)
    FOR J%=I%+2% TO LEN(TEMP$)
    IF MID$(TEMP$,J%,1%)="/" THEN EXIT FOR
    NEXT J%
    SHELLCOMMAND$=MID$(TEMP$,I%+2%,(J%-I%-2%))
    SHELLCOMMAND$=TRIM$(SHELLCOMMAND$)
    
    IF SHELLCOMMAND$="" THEN GOTO COMMANDTAILBAD
    
    TEMP$=DIR$(TAGFILE$)
    IF LEN(TEMP$)=0 THEN
    OPEN TAGFILE$ FOR OUTPUT AS #1
    PRINT #1,"this is a tag file for PROGDBNO.EXE"
    PRINT #1,"PROGDBNO.EXE is used for monitoring program instance execution"
    PRINT #1,"in other words to keep a program running more than once at the same time"
    CLOSE 1
    END IF
    
    ON ERROR GOTO TAGFILEINUSE
    OPEN TAGFILE$ FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #1
    ON ERROR GOTO 0
    SHELL SHELLCOMMAND$
    PRINT "dos exit code set at 0"
    EXITCODE%=0
    EXIT SUB
    
    TAGFILEINUSE:
    PRINT "your program was refused to run because the tag file is in use"
    PRINT "your tag file = "+TAGFILE$
    PRINT "your shell command = "+SHELLCOMMAND$
    PRINT "dos exit code set at 1"
    EXITCODE%=1
    EXIT SUB
    
    COMMANDTAILBAD:
    PRINT "PROGDBNO should have two command tail directives"
    PRINT "/t is used for the file to lock while your shelled program is running"
    PRINT "/s is the command with any command tail of any program you want to run"
    PRINT "ex: PROGDBNO /tc:\instance\prog0001.tag /sedit.com filename.txt"
    print "be sure to use the filename extension in the full executable filename unless it ends in .exe"
    EXITCODE%=0
    END SUB
    ------------------




    [This message has been edited by paul d purvis (edited June 09, 2005).]
    p purvis

    Comment


    • #3
      ..and here is how you can format that code so others can read it..

      http://www.powerbasic.com/support/forums/ubbcode.html

      You can 'edit' your messages to insert the code tags.


      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        ..and here is how you can format that code so others can read it..
        michael thanks for the tip.
        other than my poor english.
        what do you mean by the above statement
        i added in the code statement with brackets to make the print larger
        i actually prefer the smaller print so i can see more on one line
        the browsers mozilla and ie have functions to increase the size of the
        text(displayed fonts).
        i have to have reading glasses these days so i do know to the eye strain.
        thanks
        paul




        ------------------
        p purvis

        Comment


        • #5
          Paul,

          What Michael meant is that, when you post source code in these forums,
          you should start the code with [ code ] and end it with [ /code ]
          (remove the spaces inside the brackets. I had to include them so the
          forum software does not interpret them as real UBB tags).

          Including those two items will make the forum software preserve the
          indentations in the source code you post, which makes the code
          much easier to read and understand. When you do not include those items,
          then everything you write in your message, including the source code,
          will be aligned to the left. That makes it very difficult to follow
          the logic flow of the source code.


          ------------------
          clay at clayclear dot net

          Comment


          • #6
            But now that I can see the indenting....

            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              ok
              the msdos program on the first posting of this thread has been reworked a little.
              the changes i made where to improve the readabilty of the propgram
              i added an exit code 2 if the program was not able to make since of the command tail
              i added code to the program just before the normal exit, where if this program
              created a tagfile (what i call a file used for a switch(logic) function) it would
              delete it upon exiting the program or leave the tagfile if this program did not create it.

              the msdos(powerbasic 3.5) program is called progdbno
              i think a better name for the pbcc file would be wprgdbno
              thanks again for your help
              paul
              ps
              i use a program call pbsf from powerbasic somewhere i believe
              and it left justified a lot of my indenting of lines.
              does anybody else have a progam to take a powerbasic program and
              reformat its text.




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


              [This message has been edited by paul d purvis (edited June 09, 2005).]
              p purvis

              Comment


              • #8
                I have a terrific formatter for PB/DOS... I think I just sent it to someone ( a source + exe ZIP package), or maybe I sent it to PB to put in files section. (But I don't remember doing that, so I probably didn't).

                It's called "BB" (for Basic Beautify).

                If you can't find it here drop me a note.

                That will also remind me to either post it or ask PB to put it in Files Section.

                mailto:[email protected][email protected]</A>

                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  hey michael
                  i am interested
                  will it correct my english too.
                  just kidding
                  i had a look over powerbasic's web site and was not able to find
                  it.
                  i would like to put it through on these listing i have
                  thanks paul


                  ------------------
                  p purvis

                  Comment

                  Working...
                  X