Solved

Shellexecute question

Posted on 2002-06-28
3
360 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 125
Intraweb download file link ? 1 155
find a node in VST 2 71
Firemonkey webbrowser scrollbars ? 1 50
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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