|

楼主 |
发表于 2009-7-17 15:55:01
|
显示全部楼层
回 1楼(猫工) 的帖子
-
- Main Unit source code ...
- ------------------------------
- unit MainUnit;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
- Process, StdCtrls;
- const
- READ_BYTES = 2048;
- type
- { TMyThread }
- TMyThread = class(TThread)
- private
- //fStatusText: string;
- procedure ShowStatus;
- protected
- procedure Execute; override;
- public
- constructor Create(CreateSuspended: boolean);
- end;
- { TForm1 }
- TForm1 = class(TForm)
- Edit1 : TEdit;
- Memo1 : TMemo;
- Process1: TProcess;
- procedure FormCreate(Sender: TObject);
- private
- public
- end;
- var
- Form1: TForm1;
- M : TMemoryStream;
- n : LongInt;
- BytesRead : LongInt;
- implementation
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject);
- var
- MyThread : TMyThread;
- S : TStringList;
- begin
- M := TMemoryStream.Create;
- S := TStringList.Create;
- BytesRead := 0;
- MyThread := TMyThread.Create(True);
- // With the True parameter it doesn't start automatically
- //MyThread := TMyThread.Create(False);
- if Assigned(MyThread.FatalException) then
- raise MyThread.FatalException;
- // Here the code initialises anything required before the threads starts executing
- MyThread.Resume;
- //Sleep(20000);
- while Process1.Running do
- begin
- M.SetSize(BytesRead+READ_BYTES);
- n := Process1.Output.Read((M.Memory+BytesRead)^, READ_BYTES);
- if n > 0 then
- begin
- Inc(BytesRead, n);
- end
- else
- begin
- Sleep(100);
- end;
- end; {
- repeat
- M.SetSize(BytesRead + READ_BYTES);
- n := Process1.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
- if n > 0 then
- begin
- Inc(BytesRead, n);
- end;
- until n <= 0; }
- if BytesRead > 0 then WriteLn;
- M.SetSize(BytesRead);
- S.LoadFromStream(M);
- S.Free;
- M.Free;
- end;
- //Edit1.Text:='AAAAA';
- //Sleep(20000);
- end;
- { TMyThread }
- procedure TMyThread.ShowStatus;
- // this method is only called by Synchronize(@ShowStatus) and therefore
- // executed by the main thread
- // The main thread can access GUI elements, for example Form1.Caption.
- begin
- //Form1.Caption := fStatusText;
- Form1.Process1 := TProcess.Create(nil);
- {$IFDEF WIN32}
- Form1.Process1.CommandLine := 'D:\\MyDOCU\\Work\\xxxxx';
- {$ENDIF}
- Form1.Process1.Options := Form1.Process1.Options + [poWaitOnExit];
- Form1.Process1.Execute;
- Form1.Process1.Free;
- end;
- procedure TMyThread.Execute;
- //var
- //newStatus : string;
- begin
- //fStatusText := 'TMyThread Starting...';
- Synchronize(@Showstatus);
- //fStatusText := 'TMyThread Running...';
- //Process1 := TProcess.Create(nil);
- //Process1.CommandLine := 'D:\\MyDOCU\\Work\\xxxxx';
- //Process1.Options := Process1.Options + [poWaitOnExit, poUsePipes];
- //Process1.Execute;
- //Process1.Free;
- {
- while (not Terminated) and (true any condition required) do begin
- //here goes the code of the main thread loop
- newStatus:='TMyThread Time: '+FormatDateTime('YYYY-MM-DD HH:NN:SS',Now);
-
- if NewStatus <> fStatusText then begin
- fStatusText := newStatus;
- Synchronize(@Showstatus);
- end;
- end;
- }
- //Synchronize(@Showstatus);
- end;
- constructor TMyThread.Create(CreateSuspended: boolean);
- begin
- FreeOnTerminate := True;
- inherited Create(CreateSuspended);
- end;
- initialization
- {$I mainunit.lrs}
- end.
- -------------------------------------------
复制代码 |
|