2

I have a TButton in the main TForm. When user click the button, it will execute the below process:

begin
 Process_done := FALSE;
 Process_Result.Clear;
 cmdProcess.CommandLine := #34+AppPath+'getdata.exe"';
 cmdProcess.Run;
 Repeat
 Application.ProcessMessages;
 Until Process_done;
end;

As you can see above, the process calls external executable, and the process can take some times which blocking the main application.

This is only one process, and I need another one.

So, I am thinking to implement multi-threading, where I can run the above process in a separate thread. The other process as well. And the main thread can do something WHILE checking when both processes done.

Can anyone give me some examples how to do this using Delphi 7?

OR point me to an article, simple implementation like this?

Thanks.

RRUZ
137k20 gold badges364 silver badges489 bronze badges
asked Mar 9, 2012 at 7:59

2 Answers 2

15

Try something like this:

type
 TRunProcessThread = class(TThread)
 protected
 cmdProcess: Whatever;
 procedure Execute; override;
 public
 constructor Create(const ACmdLine: String);
 destructor Destroy; override;
 end;
constructor TRunProcessThread.Create(const ACmdLine: String);
begin
 inherited Create(True);
 FreeOnTerminate := True;
 cmdProcess := Whatever.Create;
 cmdProcess.CommandLine := ACmdLine;
end;
destructor TRunProcessThread.Destroy;
begin
 cmdProcess.Free;
 inherited;
end;
procedure TRunProcessThread.Execute;
begin
 cmdProcess.Run;
 ...
end;

.

procedure TForm1.Button1Click(Sender: TObject);
var
 Thread: TRunProcessThread;
begin
 Thread := TRunProcessThread.Create(AnsiQuotedStr(AppPath + 'getdata.exe', #34));
 Thread.OnTerminate := ProcessDone;
 Thread.Resume;
end;
procedure TForm1.ProcessDone(Sender: TObject);
begin
 // access TRunProcessThread(Sender) to get result information as needed ...
end;
answered Mar 9, 2012 at 9:05
1
  • 13
    You know enough to get into lots of trouble now, but not enough to get out. But there's no better way to learn (that I know of) then to experience the fun of threads, race conditions, deadlocks, resource contention, and the non-thread-safe nature of the VCL than to experience it yourself. You've just jumped down the rabbit hole. It goes very deep. Commented Mar 10, 2012 at 17:06
1

You should create a class inherited from TThread and put that code in there. I don't remember exactly, but I think you'll find TThread template in File->New dialog box. When code execution is finished, you just notify your gui. Here's an article how to synchronize UI with external thread http://delphi.about.com/od/kbthread/a/thread-gui.htm

answered Mar 9, 2012 at 9:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.