unit mythread;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
Type
TShowStatusEvent = procedure(Status: String) of Object;
TMyThread = class(TThread)
private
fStatusText : string;
FOnShowStatus: TShowStatusEvent;
procedure ShowStatus;
function ReadInfo() : String;
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
property OnShowStatus: TShowStatusEvent read FOnShowStatus write FOnShowStatus;
end;
implementation
uses process;
constructor TMyThread.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMyThread.ShowStatus;
// this method is executed by the mainthread and can therefore access all GUI elements.
begin
if Assigned(FOnShowStatus) then
begin
FOnShowStatus(fStatusText);
end;
end;
procedure TMyThread.Execute;
var
newStatus : string;
begin
fStatusText := 'TMyThread Starting...';
Synchronize(@Showstatus);
fStatusText := 'TMyThread Running...';
NewStatus := ReadInfo();
if NewStatus <> fStatusText then
begin
fStatusText := newStatus;
Synchronize(@Showstatus);
end;
end;
function TMyThread.ReadInfo(): String;
var hprocess: TProcess;
OutputLines: TStringList;
OutputError: TstringList;
begin
OutputLines:=TStringList.Create;
OutputError:=TStringList.Create;
hProcess := TProcess.Create(nil);
hProcess.Executable := '/usr/sbin/lpinfo';
hprocess.Parameters.Add('-v');
hprocess.Parameters.Add('--timeout');
hprocess.Parameters.Add('1');
hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
hProcess.Execute;
OutputLines.LoadFromStream(hprocess.Output);
OutputError.Add('Error :');
OutputError.LoadFromStream(hProcess.Stderr);
if OutputError.Text <> '' then ReadInfo := OutputError.Text
else ReadInfo := OutputLines.Text;
hProcess.Free;
OutputLines.Free;
Outputerror.Free;
end;
end.