Class tmsethread

Unit

Declaration

type tmsethread = class(TObject)

Description

Runs a method or a global function in a separate thread, so that a long operation does not block the application while it runs. The following example starts a three second simulated operation in a thread while a paint box blinks to show that the user interface stays responsive, and properly terminates the thread when the form is destroyed.

uses
  main_mfm;

{ ttestthreadcontainer }

destructor ttestthreadcontainer.Destroy;
begin
  if FThread <> nil then
  begin
    FThread.Terminate;
    application.WaitForThread(FThread);
    FThread.Destroy;
  end;
  inherited;
end;

procedure ttestthreadcontainer.Run;
begin
  FThread := tmsethread.Create(@Execute);
end;

function ttestthreadcontainer.Execute(thread: tmsethread): integer;
begin
  Sleep(3000);
  mainfo.tmWait.Enabled := FALSE;
  ShowMessage('Operation completed.');
  result := 0;
end;

{ tmainfo }

procedure tmainfo.ButtonExecute(const sender: TObject);
begin
  tmWait.Enabled := TRUE;
  FThreadContainer.Run;
end;

procedure tmainfo.FormCreate(const sender: TObject);
begin
  FThreadContainer := ttestthreadcontainer.Create();
  FBlink := FALSE;
end;

procedure tmainfo.TimerExecute(const sender: TObject);
begin
  pbWait.Invalidate;
  FBlink := not FBlink;
end;

procedure tmainfo.PaintBoxPaint(const sender: twidget; const acanvas: tcanvas);
begin
  if FBlink then
    acanvas.FillRect(MakeRect(0, 0, sender.bounds_cx, sender.bounds_cy), CL_BLUE);
end;

procedure tmainfo.FormDestroy(const sender: TObject);
begin
  FThreadContainer.Free;
end;

Hierarchy

Overview

Fields

Protected finfo: threadinfoty;
Protected fthreadproc: threadprocty;
Protected fexecresult: integer;
Protected fstate: threadstatesty;
Protected fwaitforsem: semty;

Methods

Protected function internalthreadproc: integer;
Protected function getrunning: boolean;
Protected function getterminated: boolean;
Protected function getfreeonterminate: boolean;
Protected procedure setfreeonterminate(const avalue: boolean);
Protected function execute(thread: tmsethread): integer; virtual;
Protected procedure run(); virtual;
Public constructor create; overload;
Public constructor create(const afreeonterminate: boolean; const astacksizekb: integer = 0); overload;
Public constructor create(const athreadproc: threadprocty; const afreeonterminate: boolean = false; const astacksizekb: integer = 0); overload; virtual;
Public destructor destroy; override;
Public procedure afterconstruction; override;
Public function waitfor: integer; virtual;
Public procedure terminate; virtual;
Public procedure kill;

Properties

Public property running: boolean read getrunning;
Public property terminated: boolean read getterminated;
Public property id: threadty read finfo.id;
Public property freeonterminate: boolean read getfreeonterminate write setfreeonterminate;

Description

Fields

Protected finfo: threadinfoty;

This item has no description.

Protected fthreadproc: threadprocty;

This item has no description.

Protected fexecresult: integer;

This item has no description.

Protected fstate: threadstatesty;

This item has no description.

Protected fwaitforsem: semty;

This item has no description.

Methods

Protected function internalthreadproc: integer;

This item has no description.

Protected function getrunning: boolean;

This item has no description.

Protected function getterminated: boolean;

This item has no description.

Protected function getfreeonterminate: boolean;

This item has no description.

Protected procedure setfreeonterminate(const avalue: boolean);

This item has no description.

Protected function execute(thread: tmsethread): integer; virtual;

This item has no description.

Protected procedure run(); virtual;

This item has no description.

Public constructor create; overload;

This item has no description.

Public constructor create(const afreeonterminate: boolean; const astacksizekb: integer = 0); overload;

This item has no description.

Public constructor create(const athreadproc: threadprocty; const afreeonterminate: boolean = false; const astacksizekb: integer = 0); overload; virtual;

This item has no description.

Public destructor destroy; override;

This item has no description.

Public procedure afterconstruction; override;

This item has no description.

Public function waitfor: integer; virtual;

This item has no description.

Public procedure terminate; virtual;

This item has no description.

Public procedure kill;

This item has no description.

Properties

Public property running: boolean read getrunning;

This item has no description.

Public property terminated: boolean read getterminated;

This item has no description.

Public property id: threadty read finfo.id;

This item has no description.

Public property freeonterminate: boolean read getfreeonterminate write setfreeonterminate;

This item has no description.


Generated by PasDoc 1.0.4.