Solved

Shellexecute question

Posted on 2002-06-28
3
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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