Solved

Shellexecute question

Posted on 2002-06-28
3
361 Views
Last Modified: 2010-04-04
I am writing a program to monitor certain folder(using timer control). If there are txt files coming into this folder, my program will use shellexecute to invoke another exe. file by using "shellexecute" to process the txt file, after that delete it. The problem is: after I "shellexecute", I don't know if the exe finishes running (as this exe will process the text file and insert records to the sql server database.) Since I have to delete the text file after the "shellexecute" finish processing it. That cause a lot of problems: 1. while the shellexcute still processing the file, the timer event invoke again. 2. The shellexecute still running, the text file is already being deleted.

How should I make sure the first file is being processed by shellexecute before going to the next process?

Any suggestion will be appreciated!

llucy
0
Comment
Question by:llucy88
3 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 100 total points
ID: 7116486
llucy88,

I dig up some old code that does what you want:

Copy/Paste:
------------------

function RunAndWait(theapp: string): boolean;
// returns true if successful, false otherwise.
var CmdLine: array[0..255] of char;
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
begin
  Result := false;
  StrPCopy(CmdLine,theapp);
  FillChar(StartupInfo,sizeof(StartupInfo),0);
  StartupInfo.cb := sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_SHOWNORMAL;
  if CreateProcess(nil,CmdLine,nil,nil,false,NORMAL_PRIORITY_CLASS,
                   nil,nil,StartupInfo,ProcessInfo) then
    begin
      Result := true;
      WaitForSingleObject(ProcessInfo.hThread,INFINITE);
      CloseHandle(ProcessInfo.hThread);
      CloseHandle(ProcessInfo.hProcess);
    end;
end;



DEMO:
-----------

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption:='Started!';
  RunAndWait('Notepad.exe');
  Caption:='FINISHED!';
end;
0
 

Expert Comment

by:d32coder
ID: 7116553
or  move the text file to another folder then launch the exe there.


New file goes to C:\INCOMING
Your prog moves the file to  C:\WORKING
Shellexecute the exe to the file in WORKING
The timer still looking for new files in INCOMING

The *Other* exe should delete the text file when it is done processing it.

no errors

Don
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7118419
Or ...
1. create a parameterized .CMD file that runs the program and then deletes the file.
2. shellexecute the .CMD file, passing the name of the .txt file, and forget about it.

============================================
Example of .CMD file

"C:\Program Files\App Name\Appl.exe" %1
Del %1

REM %1 is fully qualified path of .txt file
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 89
Tembedded WB animatid gifs not animated on some pcs 2 86
JAudiorecorder record freezing the app 29 83
Wincontrol not (correctly) drawn 15 48
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question