1 | unit UOperationProgress;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | ExtCtrls, Process;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TOperationProgressForm }
|
---|
14 |
|
---|
15 | TOperationProgressForm = class(TForm)
|
---|
16 | ButtonAbort: TButton;
|
---|
17 | ButtonClose: TButton;
|
---|
18 | Edit1: TEdit;
|
---|
19 | Memo1: TMemo;
|
---|
20 | Timer1: TTimer;
|
---|
21 | procedure ButtonAbortClick(Sender: TObject);
|
---|
22 | procedure ButtonCloseClick(Sender: TObject);
|
---|
23 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
24 | procedure FormShow(Sender: TObject);
|
---|
25 | procedure Timer1Timer(Sender: TObject);
|
---|
26 | private
|
---|
27 | Process: TProcess;
|
---|
28 | public
|
---|
29 | CommandLine: string;
|
---|
30 | Path: string;
|
---|
31 | Environment: string;
|
---|
32 | procedure Perform;
|
---|
33 | end;
|
---|
34 |
|
---|
35 | var
|
---|
36 | OperationProgressForm: TOperationProgressForm;
|
---|
37 |
|
---|
38 | implementation
|
---|
39 |
|
---|
40 | {$R *.lfm}
|
---|
41 |
|
---|
42 | { TOperationProgressForm }
|
---|
43 |
|
---|
44 | procedure TOperationProgressForm.ButtonAbortClick(Sender: TObject);
|
---|
45 | begin
|
---|
46 | if Assigned(Process) then
|
---|
47 | Process.Terminate(0);
|
---|
48 | end;
|
---|
49 |
|
---|
50 | procedure TOperationProgressForm.ButtonCloseClick(Sender: TObject);
|
---|
51 | begin
|
---|
52 |
|
---|
53 | end;
|
---|
54 |
|
---|
55 | procedure TOperationProgressForm.FormClose(Sender: TObject;
|
---|
56 | var CloseAction: TCloseAction);
|
---|
57 | begin
|
---|
58 | Memo1.Clear;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure TOperationProgressForm.FormShow(Sender: TObject);
|
---|
62 | begin
|
---|
63 | Memo1.Clear;
|
---|
64 | ButtonAbort.Enabled := False;
|
---|
65 | ButtonClose.Enabled := False;
|
---|
66 | Timer1.Enabled := True;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TOperationProgressForm.Timer1Timer(Sender: TObject);
|
---|
70 | begin
|
---|
71 | Timer1.Enabled := False;
|
---|
72 | ButtonAbort.Enabled := True;
|
---|
73 | Perform;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | procedure TOperationProgressForm.Perform;
|
---|
77 | var
|
---|
78 | Buffer: string;
|
---|
79 | Count: Integer;
|
---|
80 | Text: string;
|
---|
81 | Line: string;
|
---|
82 | begin
|
---|
83 | Text := '';
|
---|
84 | try
|
---|
85 | Process := TProcess.Create(nil);
|
---|
86 | if Path <> '' then
|
---|
87 | Process.CurrentDirectory := Path;
|
---|
88 | Path := '';
|
---|
89 | if Environment <> '' then
|
---|
90 | Process.Environment.Text := Environment;
|
---|
91 | Environment := '';
|
---|
92 | Process.CommandLine := CommandLine;
|
---|
93 | Edit1.Text := CommandLine;
|
---|
94 | Process.Options := [poUsePipes, poNoConsole];
|
---|
95 | Process.Execute;
|
---|
96 | Application.ProcessMessages;
|
---|
97 | while Process.Running or (Process.Output.NumBytesAvailable > 0) or
|
---|
98 | (Process.Stderr.NumBytesAvailable > 0) do
|
---|
99 | begin
|
---|
100 | if Process.Output.NumBytesAvailable > 0 then begin
|
---|
101 | SetLength(Buffer, 1000);
|
---|
102 | Count := Process.Output.Read(Buffer[1], Length(Buffer));
|
---|
103 | SetLength(Buffer, Count);
|
---|
104 | Text := Text + Buffer;
|
---|
105 | while Pos(LineEnding, Text) > 0 do begin
|
---|
106 | Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
|
---|
107 | Delete(Text, 1, Length(Line) + Length(LineEnding));
|
---|
108 | Memo1.Lines.Add(Line);
|
---|
109 | end;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | if Process.Stderr.NumBytesAvailable > 0 then begin
|
---|
113 | SetLength(Buffer, 1000);
|
---|
114 | Count := Process.Stderr.Read(Buffer[1], Length(Buffer));
|
---|
115 | SetLength(Buffer, Count);
|
---|
116 | Text := Text + Buffer;
|
---|
117 | while Pos(LineEnding, Text) > 0 do begin
|
---|
118 | Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
|
---|
119 | Delete(Text, 1, Length(Line) + Length(LineEnding));
|
---|
120 | Memo1.Lines.Add(Line);
|
---|
121 | end;
|
---|
122 | end;
|
---|
123 | Sleep(10);
|
---|
124 | Application.ProcessMessages;
|
---|
125 | end;
|
---|
126 | finally
|
---|
127 | Memo1.Lines.Add(Text);
|
---|
128 | FreeAndNil(Process);
|
---|
129 | end;
|
---|
130 | ButtonAbort.Enabled := False;
|
---|
131 | ButtonClose.Enabled := True;
|
---|
132 | ButtonAbort.ModalResult := mrOK;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | end.
|
---|
136 |
|
---|