***2023-08-05更新了uInfoHub.pas。
Delphi通常使用TThread.Synclonize把线程信息显示到界面上,但由于此方法需与主界面同步,会拖慢线程的运行,如果显示的信息比较多或更新很快,对程序的性能会有比较大的影响。
此框架使用PostMessage异步方式,最大程度地提高了程序的性能,并保证了信息显示的顺序。

unit uMainForm; //示例
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls,
uDemoThread, Data.DB, Vcl.Grids, Vcl.DBGrids, Vcl.ComCtrls, Datasnap.DBClient;
type
TMainForm = class(TForm)
ButtonRun: TButton;
Panel1: TPanel;
Memo1: TMemo;
Panel2: TPanel;
Splitter1: TSplitter;
ListView1: TListView;
Splitter2: TSplitter;
DBGrid1: TDBGrid;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
ClientDataSet1id: TIntegerField;
ClientDataSet1name: TStringField;
procedure ButtonRunClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
LogInfoThread: TLogInofThread;
DBGridInfoThread: TDBGridInfoThread;
ListViewInfoThread: TListViewInfoThread;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutDown := True;
with ClientDataSet1 do
begin
CreateDataSet;
LogChanges := False;
Open;
end;
Position := poScreenCenter;
end;
procedure TMainForm.ButtonRunClick(Sender: TObject);
begin
LogInfoThread := TLogInofThread.Create;
DBGridInfoThread := TDBGridInfoThread.Create;
ListViewInfoThread := TListViewInfoThread.Create;
ButtonRun.Enabled := False;
end;
end.
object MainForm: TMainForm
Left = 0
Top = 0
Anchors = [akLeft, akTop, akRight, akBottom]
Caption = 'Demo'
ClientHeight = 496
ClientWidth = 565
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
DesignSize = (
565
496)
PixelsPerInch = 96
TextHeight = 13
object ButtonRun: TButton
Left = 492
Top = 462
Width = 65
Height = 25
Anchors = [akRight, akBottom]
Caption = 'Run'
TabOrder = 0
OnClick = ButtonRunClick
end
object Panel1: TPanel
Left = 8
Top = 8
Width = 549
Height = 448
Anchors = [akLeft, akTop, akRight, akBottom]
BevelInner = bvRaised
BevelOuter = bvLowered
TabOrder = 1
object Splitter1: TSplitter
Left = 2
Top = 317
Width = 545
Height = 3
Cursor = crVSplit
Align = alBottom
ExplicitTop = 2
ExplicitWidth = 318
end
object Memo1: TMemo
Left = 2
Top = 320
Width = 545
Height = 126
Align = alBottom
TabOrder = 0
end
object Panel2: TPanel
Left = 2
Top = 2
Width = 545
Height = 315
Align = alClient
BevelInner = bvLowered
BevelOuter = bvLowered
TabOrder =

本文介绍了一种使用Delphi进行线程信息显示的方法,通过PostMessage异步方式提高程序性能,确保信息顺序显示,避免了TThread.Synchronize带来的性能瓶颈。
最低0.47元/天 解锁文章
656

被折叠的 条评论
为什么被折叠?



