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).]
Comment