jjw

写给自己的博客。 记录学习的点滴以备查。
随笔 - 127, 文章 - 0, 评论 - 8, 阅读 - 62632
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

delphi 多线程之System.TMonitor

Posted on   jjw  阅读(2197)  评论(0编辑  收藏  举报

三天不写代码就手生! 把测试代码记录下来。

复制代码
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Memo1: TMemo;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FFlag: Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Log(Msg: string);
begin
  TThread.Synchronize(nil, procedure
    begin
      Form1.Memo1.Lines.Add(Msg);
    end);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //错误
  if  System.MonitorTryEnter(Form1) then
  begin
    System.MonitorEnter(Form1);
    System.MonitorPulseAll(Form1);
    System.MonitorExit(Form1);
  end
  else
    Log('????');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //正确
  System.MonitorPulseAll(Form1);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  AThread: TThread;
begin
  FFlag := True;
  TThread.CreateAnonymousThread(procedure
    begin
      while True do
      begin
        if Form1.CheckBox2.Checked then
          Exit;

        if Form1.FFlag then
        begin
            System.MonitorEnter(Form1);  //必须
            Log('Thread1 Enter');
            System.MonitorWait(Form1, INFINITE);
            Log('Thread1 WaitFor');
            System.MonitorExit(Form1);   //必须
            Log('Thread1 Exit');
        end;

        Log(DateTimeToStr(Now));
        TThread.Sleep(100);
      end;
      Log('Thread Exit');
    end).Start;

  TThread.CreateAnonymousThread(procedure
    begin
      while True do
      begin
        if Form1.CheckBox2.Checked then
          Exit;

        if Form1.FFlag then
        begin
            System.MonitorEnter(Form1);
            Log('Thread2 Enter');
            System.MonitorWait(Form1, INFINITE);
            Log('Thread2 WaitFor');
            System.MonitorExit(Form1);
            Log('Thread2 Exit');
        end;

        Log(DateTimeToStr(Now));
        TThread.Sleep(100);
      end;
      Log('Thread Exit');
    end).Start;
end;

end.
复制代码

注意事项:

1. 三个方法必须一块使用,不能只写 System.MonitorWait(Form1, INFINITE);

            System.MonitorEnter(Form1);  //必须
            Log('Thread1 Enter');
            System.MonitorWait(Form1, INFINITE);
            Log('Thread1 WaitFor');
            System.MonitorExit(Form1);   //必须
            Log('Thread1 Exit');

2.  System.MonitorPulseAll(Form1) 即可。不要这样写

    System.MonitorEnter(Form1);
    System.MonitorPulseAll(Form1);
    System.MonitorExit(Form1);

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示