source: trunk/Forms/UFormConsole.pas

Last change on this file was 16, checked in by chronos, 9 years ago
  • Modified: Execute get log and get status silently without dialog.
File size: 3.8 KB
Line 
1unit UFormConsole;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ExtCtrls, Process;
10
11type
12
13 { TFormConsole }
14
15 TFormConsole = class(TForm)
16 ButtonAbort: TButton;
17 EditCommand: TEdit;
18 Memo1: TMemo;
19 Timer1: TTimer;
20 procedure ButtonAbortClick(Sender: TObject);
21 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
22 procedure FormCreate(Sender: TObject);
23 procedure FormDestroy(Sender: TObject);
24 procedure FormShow(Sender: TObject);
25 procedure Timer1Timer(Sender: TObject);
26 private
27 Process: TProcess;
28 { private declarations }
29 public
30 Executable: string;
31 Parameters: TStringList;
32 WorkingDir: string;
33 EnvironmentVariables: TStringList;
34 Aborted: Boolean;
35 Log: TStringList;
36 procedure Perform;
37 end;
38
39var
40 FormConsole: TFormConsole;
41
42implementation
43
44{$R *.lfm}
45
46{ TFormConsole }
47
48procedure TFormConsole.FormCreate(Sender: TObject);
49begin
50 Parameters := TStringList.Create;
51 Log := TStringList.Create;
52 EnvironmentVariables := TStringList.Create;
53end;
54
55procedure TFormConsole.FormDestroy(Sender: TObject);
56begin
57 EnvironmentVariables.Free;
58 Log.Free;
59 Parameters.Free;
60end;
61
62procedure TFormConsole.FormShow(Sender: TObject);
63begin
64 ButtonAbort.Enabled := False;
65 Timer1.Enabled := True;
66end;
67
68procedure TFormConsole.Timer1Timer(Sender: TObject);
69begin
70 Timer1.Enabled := False;
71 Perform;
72end;
73
74procedure TFormConsole.ButtonAbortClick(Sender: TObject);
75begin
76 if Assigned(Process) then begin
77 Aborted := True;
78 Process.Terminate(0);
79 end;
80end;
81
82procedure TFormConsole.FormClose(Sender: TObject; var CloseAction: TCloseAction
83 );
84begin
85 ButtonAbort.Click;
86end;
87
88procedure TFormConsole.Perform;
89var
90 Buffer: string;
91 Count: Integer;
92 Text: string;
93 Line: string;
94 CommandLine: string;
95 I: Integer;
96begin
97 Text := '';
98 try
99 Process := TProcess.Create(nil);
100 Aborted := False;
101 ButtonAbort.Enabled := True;
102 Log.Clear;
103 Memo1.Clear;
104 if WorkingDir <> '' then
105 Process.CurrentDirectory := WorkingDir;
106 Process.Environment.Assign(EnvironmentVariables);
107 Process.Parameters.Assign(Parameters);
108 Process.Executable := Executable;
109 CommandLine := Executable + ' ' + StringReplace(Parameters.Text, LineEnding, ' ', [rfReplaceAll]);
110 if CommandLine[Length(CommandLine)] = LineEnding then
111 SetLength(CommandLine, Length(CommandLine) - 1);
112 EditCommand.Text := CommandLine;
113 Process.Options := [poUsePipes, poNoConsole];
114 Process.Execute;
115 Application.ProcessMessages;
116 while Process.Running or (Process.Output.NumBytesAvailable > 0) or
117 (Process.Stderr.NumBytesAvailable > 0) do
118 begin
119 if Process.Output.NumBytesAvailable > 0 then begin
120 SetLength(Buffer, 1000);
121 Count := Process.Output.Read(Buffer[1], Length(Buffer));
122 SetLength(Buffer, Count);
123 Text := Text + Buffer;
124 while Pos(LineEnding, Text) > 0 do begin
125 Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
126 Delete(Text, 1, Length(Line) + Length(LineEnding));
127 if Visible then Memo1.Lines.Add(Line);
128 Log.Add(Line);
129 end;
130 end;
131
132 if Process.Stderr.NumBytesAvailable > 0 then begin
133 SetLength(Buffer, 1000);
134 Count := Process.Stderr.Read(Buffer[1], Length(Buffer));
135 SetLength(Buffer, Count);
136 Text := Text + Buffer;
137 while Pos(LineEnding, Text) > 0 do begin
138 Line := Copy(Text, 1, Pos(LineEnding, Text) - 1);
139 Delete(Text, 1, Length(Line) + Length(LineEnding));
140 if Visible then Memo1.Lines.Add(Line);
141 Log.Add(Line);
142 end;
143 end;
144 Sleep(10);
145 Application.ProcessMessages;
146 end;
147 finally
148 if Visible then Memo1.Lines.Add(Text);
149 Log.Add(Text);
150 FreeAndNil(Process);
151 end;
152 ButtonAbort.Enabled := False;
153 //ModalResult := mrOK;
154 //Close;
155end;
156
157end.
158
Note: See TracBrowser for help on using the repository browser.