Solved

Shellexecute question

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now