Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait until file is created

Status
Not open for further replies.

harrisonj

IS-IT--Management
Dec 16, 2003
12
US
List,
I'm looking for the code (loop) to cause my program to wait until a text file is created, before it loads the text file contents into a TMemo.
Here's the deal;
My Delphi code gathers user information, then executes a fortran program which crunches numbers. The fortran program then outputs to a text file. Next, the program reads the text file into a TMemo. This is done with a simple button press in the GUI. I want to automate this, but the problem I'm having is, the code loads the TMemo field before the text file is created. I don't know how to write the code to keep the program running and waiting for the text file to be created BEFORE I import the text file into the TMemo field.

Thanks in advance,
Harrison
 
I presume from what you are saying that the Fortran program shuts itself down after crunching those numbers.
You could use WaitForProcess to wait until it has shut down
and then load it into the memo, try searching the forums for WaitForProcess Im sure there is an example about.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Harrison:

I found this code somewhere in this forum and have used it successfully. It will start the program you pass it and wait for it to finish executing. Maybe you can adapt the WaitForSingleObject to your code.

Code:
function WinExecAndWait32(FileName: string; Visibility: Integer): dWord;
var
  zAppName: array[0..512] of Char;
  zCurDir: array[0..255] of Char;
  WorkDir: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;


begin
  StrPCopy(zAppName, FileName);
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
(*
SW_HIDE			Hides the window and activates another window.
SW_MAXIMIZE		Maximizes the specified window.
SW_MINIMIZE		Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE		Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW			Activates the window and displays it in its current size and position. 
SW_SHOWDEFAULT		Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. 
SW_SHOWMAXIMIZED	Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED	Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE	Displays the window as a minimized window. The active window remains active.
SW_SHOWNA		Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE	Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL		Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
*)
  if not CreateProcess(nil,
           zAppName, { pointer to command line string }
           nil, { pointer to process security attributes }
           nil, { pointer to thread security attributes }
           false, { handle inheritance flag }
           CREATE_NEW_CONSOLE or { creation flags }
           NORMAL_PRIORITY_CLASS,
           nil, { pointer to new environment block }
           nil, { pointer to current directory name }
           StartupInfo, { pointer to STARTUPINFO }
           ProcessInfo) then
    Result := 0 { pointer to PROCESS_INF }
  else
  begin
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
  end;
end;

Regards and HTH,
JGS
 
Or another yet cheesy way to do it is have a timer set to something like 500 ms and use FileAge (use FileTimeToDateTime to be able to calculated with it) and then you can calculate how old the file is by extracting the Time part from it, to see if it is the same as the current time. Cheesy but it works and allot simpler to implement.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Thanks for all the repsponses.

---Harrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top