【Delphi】以高性能方式把线程信息显示在界面上的一个通用小框架【2023-08-05更新】

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

***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 =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值