Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Jul : Calling a batch file in a thread doesn't work

www.cryer.info
Managed Newsgroup Archive

Calling a batch file in a thread doesn't work

Subject:Calling a batch file in a thread doesn't work
Posted by:"Stephan Schneider" (stephan.schneid..@web.de)
Date:Wed, 6 Jul 2005 08:50:13

Hallo,

Env.: Delphi 6 Ent. UP2

In my application I call several batch files, after each other. I wrote a
TThread descendant, which a) executes the batch file and b) waits for its
return. Even if the first batch is done, the second batch can be executed,
and so on.

A function ExecAppAndWait() looks like:

function ExecAppAndWait(const sApp, sParams: String; wShow: Word; bWait:
Boolean = True): Cardinal;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  hProc: THandle;
  CmdLine: String;
begin
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.wShowWindow := wShow;
  CmdLine := sApp + ' ' + sParams;
  Win32Check(CreateProcess(nil, PChar(CmdLine), nil, nil, False,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo));
  hProc := ProcessInfo.hProcess;
  CloseHandle(ProcessInfo.hThread);
  if bWait then
    if WaitForSingleObject(hProc, INFINITE) <> WAIT_FAILED then
      GetExitCodeProcess(hProc, Result);
  CloseHandle(hProc);
end;

The Execute method of the TThread descendant looks like:

procedure TMyThread.Execute;
begin
  if not Terminated then begin
    Synchronize(SetStartTime);
    ExecAppAndWait(FFileName, '', SW_SHOWNORMAL);
    Synchronize(SetEndTime);
  end;
end;

For example, the content of a batch file is:
md rd
program1.exe
copy a.txt b.org
program2.exe 1 2


Now I've the following problem:
If I run the thread inside my app, the batch file is in fact executed, but
the content inside the batch file isn't done. If I start the batch file
manually, all is done well.

I've further done a little test:
When I change the content of the batch file, e.g. in calling
C:\Windows
otepad.exe the batch file and it's content will be executed
inside my application, in the strict sense inside my thread. Notepad
appears, and when I close Notepad, the Endtime is set (like in my thread
execute). All works fine. But the "real" content of the batch file aren't
executed.

Why?

Kind regards
Stephan

Replies:

www.cryer.info
Managed Newsgroup Archive