⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listmonitor_mainform.pas

📁 source code for the Marco Cantu s book Delphi 2009 Handbook
💻 PAS
字号:
unit ListMonitor_MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Generics.Collections;

type
  TFormListMonitor = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    ListBox3: TListBox;
    btnStartThreads: TButton;
    btnStatus: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnStartThreadsClick(Sender: TObject);
    procedure btnStatusClick(Sender: TObject);
  private
    fThreads: TObjectList<TThread>; // owns and destroys
    fListBoxes: TList<TListBox>;
  public
    { Public declarations }
  end;

var
  FormListMonitor: TFormListMonitor;

implementation

{$R *.dfm}

uses
  ListMonitor_Threads;

procedure TFormListMonitor.btnStartThreadsClick(Sender: TObject);
var
  I: Integer;
begin
  for I := 1 to 3 do
    fThreads.Add (TAddToListThread.Create (False));
end;

procedure TFormListMonitor.btnStatusClick(Sender: TObject);
var
  aListBox: TListBox;
begin
  for aListBox in fListBoxes do
  begin
    if System.TMonitor.TryEnter(aListBox) then
    try
      aListBox.Items.Add('Available');
    finally
      System.TMonitor.Exit(aListBox);
    end;
  end;
end;

procedure TFormListMonitor.FormCreate(Sender: TObject);
begin
  fThreads := TObjectList<TThread>.Create;
  fListBoxes := TList<TListBox>.Create;
  fListBoxes.Add (ListBox1);
  fListBoxes.Add (ListBox2);
  fListBoxes.Add (ListBox3);
end;

procedure TFormListMonitor.FormDestroy(Sender: TObject);
begin
  // ends all threads in a clean, way...
  // ... but this can take a lot of time
  // fThreads.Free;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -