unit FormMain;
interface
uses
ThreadedJobQueue, ProcJob,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TMainForm =
class( TForm )
Button1 : TButton;
CheckBox1 : TCheckBox;
CheckBox2 : TCheckBox;
CheckBox3 : TCheckBox;
ListBox1 : TListBox;
procedure Button1Click( Sender : TObject );
private
FJobQueue : TThreadedJobQueue;
procedure LogStr(
const AStr :
string );
public
procedure AfterConstruction;
override;
procedure BeforeDestruction;
override;
end;
var
MainForm : TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.AfterConstruction;
begin
inherited;
FJobQueue := TThreadedJobQueue.Create;
end;
procedure TMainForm.BeforeDestruction;
begin
inherited;
FJobQueue.Free;
end;
procedure TMainForm.Button1Click( Sender : TObject );
begin
if FJobQueue.IsWorking
then
begin
LogStr( '
Nicht hektisch werden :o)' );
Exit;
end;
if CheckBox1.Checked
then
FJobQueue.AddJob( TProcJob.Construct(
procedure
begin
LogStr( '
Start 1' );
Sleep( 1000 );
LogStr( '
Ende 1' );
end ) );
if CheckBox2.Checked
then
FJobQueue.AddJob( TProcJob.Construct(
procedure
begin
LogStr( '
Start 2' );
Sleep( 1000 );
LogStr( '
Ende 2' );
end ) );
if CheckBox3.Checked
then
FJobQueue.AddJob( TProcJob.Construct(
procedure
begin
LogStr( '
Start 3' );
Sleep( 1000 );
LogStr( '
Ende 3' );
end ) );
FJobQueue.ProcessJobs;
end;
procedure TMainForm.LogStr(
const AStr :
string );
begin
// Diese Methode achtet von selber darauf,
// dass der eigentlich Zugriff im MainThread-Context erfolgt
if MainThreadID = GetCurrentThreadId
then
begin
ListBox1.ItemIndex := ListBox1.Items.Add( AStr );
end
else
TThread.Queue(
nil,
procedure
begin
LogStr( AStr );
end );
end;
end.