Board index » delphi » createprocess

createprocess

Firstly,I wish to apologise for a "long" posting but I need some help
here.And I apologise for the waffling first but I do
explain the problem at the end.(thanks for your patience)...!

A little while ago I sent some postings in about some code that I had come
across,
namely, the following bit of code,
[snip]
function TForm.RunAndWait(const filename:String):DWORD;
var
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
begin
Result:=STILL_ACTIVE;
GetStartupInfo(StartupInfo);
   if CreateProcess(nil,PChar(filename),nil,nil,IsConsole,
       NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInfo)
    then try
         if WaitForSingleObject(ProcessInfo.hProcess,INFINITE)=WAIT_OBJECT_0
           then GetExitCodeProcess(ProcessInfo.hProcess,Result);
         finally
           CloseHandle(ProcessInfo.hThread);
           CloseHandle(ProcessInfo.hProcess);
         end;
end;
[end snip]

I got a response of
[snip]
 It starts an application specified by the 'filename' parameter
 ("CreateProcess...") , and stops at the line
   "WaitForSingleObject..."
 until the launched application is closed.
[end snip]

Now, then...with a basic understanding of the above I set to work creating a
project which would be useful, running for our network. The basic
functionality of the project is described below...

1) the program sits on a windows workstation, running all the time scanning
for files to be found
 a directory on a novell server
2) at specified intervals, controlled by a timer, it checks to see if there
are a files being created which
have a file extension of "*.%??. The '%' denotes a printable file from
system. The '??' can be any
number from  01 to 09 which designates a print queue. e.g. if a file is
found with extension .%05 it would be
designated for a specific print queue in the system, which in turn is
connected to a particular printer in the system.
If a file is found with an extension of .%02, it relates to a different
queue and thus, a different printer.
3) to get something printed, the prg creates/modifies a batch file (inserts
filename of the file to print, queue number etc)
...the batch file incorporates the NPRINT (novell) command together with the
filename, queue number etc. When
created/modified, the batch file is run as part of a seperate process.
4) the prg waits until the file has been printed, then deleted and returns
to scan for another file
5) the prg is regarded as a sort of network print spooler

here are some code extracts

1)The program starts by reading in the path of the directory to scan, sets
the spooler running (default) etc
Application.ProcessMessages;
spoolTimer.enabled:=true;
spoolerRunning:=true;
statusbar1.simpletext:='spooler is running ...';
etc..

2) When the spooler is started or stopped, it writes the date/time etc to a
file ('spooler.log')
[extract : starting and stopping the 'spooler'. (The 'spooler' is a timed
procedure)]
procedure TsplForm.startspoolerButtonClick(Sender: TObject);
var
   G:TextFile;
begin
if spoolerRunning=false
then begin
     spoolerRunning:=true;
     spooltimer.enabled:=true;
     statusbar1.simpletext:=' The spooler is running';
     AssignFile(G,'spooler.roo');
     Append(G);
     Writeln(G,' >>> Spooler session started : '+datetostr(date)+'
'+timetostr(time));
        (the above line signals to a file that the spooler has started, from
a button click)

     Flush(G);
     CloseFile(G);
     end;
end;

procedure TsplForm.stopspoolerButtonClick(Sender: TObject);
var
   G:TextFile;
begin
Application.ProcessMessages;
spooltimer.enabled:=false;
statusbar1.simpletext:=' The spooler is not running';
if spoolerRunning=true
then begin
     spoolerRunning:=false;
     AssignFile(G,'spooler.roo');
     Append(G);
        try
           Writeln(G,' <<< Spooler session stopped : '+datetostr(date)+'
'+timetostr(time));
            (the above line signals to a file that the spooler has been
stopped, button click!)

           Flush(G);
        finally
           CloseFile(G);
        end;
     end;
end;

Here are the 'guts' of the prg.

4) This routine, on a timer, looks for a file of extension '%.??' in a
specific location (directory path).
when found the file's extension is extracted to assess which designated
queue it relates to.

[extract : ]

procedure TsplForm.spoolTimerTimer(Sender: TObject);
var
   F,G:TextFile;
   fhandle:integer;
   mysearchrec:TSearchRec;
   newfileextnumber,foundit:integer;
   newfileext:string;
   foundqueue:array [0..20] of char;
   present:TDateTime;
   hour,min,sec,msec:Word;
   disHour,disMin,disSec:integer;
begin
Application.ProcessMessages;

(section-"find the files")

     if findfirst(spoolPath+'\*.%??',faAnyFile,mysearchrec)=0
     then try
              repeat
                    fhandle:=fileopen(mysearchrec.name,fmOpenRead or
fmShareDenyWrite);
                    if fhandle>0
                    then begin
                         fileclose(fhandle);
                         panel3.caption:=' Found file '+mysearchrec.name+'
size = '+inttostr(mysearchrec.size)+' in bytes';
                         panel4.caption:=' '+datetostr(date)+'
'+timetostr(time);

                         statusbar1.simpletext:='...processing spool
files... ';
                         foundit:=pos('.',mysearchrec.name);

(section - "assess the designated queue from the file's extension")

newfileext:=copy(extractfileext(mysearchrec.name),3,2);
                         newfileextnumber:=strtoint(newfileext);
                             case newfileextnumber of
                                    01:foundqueue:='SALES';
                                    02:foundqueue:='SALES';
                                    05:foundqueue:='REPS';
                                    06:foundqueue:='LASER';
                                    07:foundqueue:='LASER';
                                    10:foundqueue:='PRINT';
                                    11:foundqueue:='PRINT';
                             else
                                    foundqueue:='PRINT';
                             end;

(section - "write into a batch file, novell's NPRINT command statement
following by the filename and extension,
    the designated queue number and how many copies of the file to print".
close/save batch file)

                         Assignfile(F,'grcspool.bat');
                         Rewrite(F);
                         Writeln(F,'NPRINT
'+copy(mysearchrec.name,0,foundit)+'%%'+newfileext+' Q='+foundqueue+' C=1
NoB NFF');
                         Closefile(F);

(section - write a log of the printed file into a log file")

                         if fileexists('spooler.roo')
                         then begin
                              AssignFile(G,'spooler.roo');
                              Append(G);
                              Writeln(G,' Log entry  : '+datetostr(date)+'
'+timetostr(time)+'   filename '+mysearchrec.name+'  filesize
'+inttostr(mysearchrec.size)+' queued to '+foundqueue);
                              Flush(G);
                              CloseFile(G);
                              end
                         else begin
                              AssignFile(G,'spooler.roo');
                              Rewrite(G);
                              CloseFile(G);
                              end;

(section - "call the batch file to print the file, but return when it's
finished. delete the file as it's no longer needed")

                              RunAndWait(spoolPath+'\grcspool.bat');
                              deletefile(mysearchrec.name);
                              sleep(2000);
                              sleep(2000);
                         end;
              until findnext(mysearchrec)<>0;
          finally
              findclose(mysearchrec);
          end
     else
          statusbar1.simpletext:='...waiting for additional spool
files....';
end;

[extract : ]

function TsplForm.RunAndWait(const filename:String):DWORD;
var
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
begin
Application.ProcessMessages;
Result:=STILL_ACTIVE;
GetStartupInfo(StartupInfo);
   if CreateProcess(nil,PChar(filename),nil,nil,IsConsole,
       NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInfo)
    then try
         if WaitForSingleObject(ProcessInfo.hProcess,INFINITE)=WAIT_OBJECT_0
           then GetExitCodeProcess(ProcessInfo.hProcess,Result);
         finally
           CloseHandle(ProcessInfo.hThread);
           CloseHandle(ProcessInfo.hProcess);
         end;

end;

As it stands the program runs in the background on my machine all the time
and it works well. It does it's job of  file selection, distribution,
printing etc. However, the "RunAndWait" routine gives me a headache becuase
each time it is called upon to print a file, call a batch file etc, it takes
the focus away from whatever I am working on!...and with it any keypresses
that I have made during that time. For example, whilst I've been typing this
posting the spooler has been running in the background. Each time it finds a
file to print, I lose focus here and discover that when the spooler returns
it has left me with some gaps, due to
the loss of focus...keypresses are lost! and I have to keep retyping.

example...

So, instead of getting a normal "the quick brown fox jumped over the lazy
dog". If the spooler 'kicks in" half way
through I get something like "the qbron fox jumpr the lazy do"

So, after all this waffle, can anyone please help me with this problem of
"losing focus" ? I need to ensure that my spooler
program runs without interrupting any other program.

thank you for your patience!

R.O.Bot

 

Re:createprocess


Add an extra line after GetStartupInfo(StartupInfo):

StartupInfo.wShowWindow := SW_SHOWMINNOACTIVE;

"CarbonCopyDroid" <C...@clones.co.uk> schreef in bericht
news:ukhY6.1464$cw.179871@monolith.news.easynet.net...
[...]

Quote
> However, the "RunAndWait" routine gives me a headache becuase
> each time it is called upon to print a file, call a batch file etc, it
takes
> the focus away from whatever I am working on!...and with it any keypresses
> that I have made during that time. For example, whilst I've been typing
this
> posting the spooler has been running in the background. Each time it finds
a
> file to print, I lose focus here and discover that when the spooler
returns
> it has left me with some gaps, due to
> the loss of focus...keypresses are lost! and I have to keep retyping.

[...]

Re:createprocess


"No wonder they call you the great oracle !"
I'd just like to say "thankyou, your code worked beautifully !"

Quote
M.H. Avegaart <avega...@NOSPAMmccomm.nl> wrote in message

news:9gs7v8$2v4r$1@scavenger.euro.net...
Quote
> Add an extra line after GetStartupInfo(StartupInfo):

> StartupInfo.wShowWindow := SW_SHOWMINNOACTIVE;

> "CarbonCopyDroid" <C...@clones.co.uk> schreef in bericht
> news:ukhY6.1464$cw.179871@monolith.news.easynet.net...
> [...]
> > However, the "RunAndWait" routine gives me a headache becuase
> > each time it is called upon to print a file, call a batch file etc, it
> takes
> > the focus away from whatever I am working on!...and with it any
keypresses
> > that I have made during that time. For example, whilst I've been typing
> this
> > posting the spooler has been running in the background. Each time it
finds
> a
> > file to print, I lose focus here and discover that when the spooler
> returns
> > it has left me with some gaps, due to
> > the loss of focus...keypresses are lost! and I have to keep retyping.
> [...]

Other Threads