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

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();
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();
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);

delphi 多线程之System.TMonitor的更多相关文章

  1. delphi 多线程之System.TMonitor (续一)

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  2. 【C#】线程之Parallel

    在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...

  3. 多线程之join方法

    join方法的功能就是使异步执行的线程变成同步执行.也就是说,当调用线程实例的start方法后,这个方法会立即返回,如果在调用start方法后后需要使用一个由这个线程计算得到的值,就必须使用join方 ...

  4. JAVA多线程之wait/notify

    本文主要学习JAVA多线程中的 wait()方法 与 notify()/notifyAll()方法的用法. ①wait() 与 notify/notifyAll 方法必须在同步代码块中使用 ②wait ...

  5. JAVA多线程之volatile 与 synchronized 的比较

    一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...

  6. java多线程之yield,join,wait,sleep的区别

    Java多线程之yield,join,wait,sleep的区别 Java多线程中,经常会遇到yield,join,wait和sleep方法.容易混淆他们的功能及作用.自己仔细研究了下,他们主要的区别 ...

  7. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  8. Java基础-进程与线程之Thread类详解

    Java基础-进程与线程之Thread类详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程与线程的区别 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 ...

  9. Android 多线程之IntentService 完全详解

    关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...

随机推荐

  1. OpenGL 3D旋转的木箱

    学习自: https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/#3d 0,首先添加glm ...

  2. windows下端口占用处理工具

    一.通用方法 经常,我们在启动应用的时候发现系统需要的端口被别的程序占用,笔者在最近使用tomcat时,老是会遇到这种端口占用的问题,如何知道谁占有了我们需要的端口,很多人都比较头疼,以下是通用方法: ...

  3. 设置Linux系统的LANG变量

    设置linux系统的LANG变量 对于国内的Linux用户,经常烦恼的一个问题是:系统常常在需要显示中文的时候却显示成了乱码,而由于某些原因,需要英文界面的系统的时候,却苦于系统不能正常输入和显示中文 ...

  4. python阅读目录

    一.python基础--列表.元祖.字典.集合 二.1231 三.12121

  5. [转]vux使用教程

    原文:https://blog.csdn.net/revival_liang/article/details/78267992 <1>. 在项目里安装vuxnpm install vux ...

  6. wxPython制作跑monkey工具(python3)

    一. wxPython制作跑monkey工具python文件源代码内容Run Monkey.py如下: #!/usr/bin/env python import wx import os import ...

  7. Tecplot: 多截面云图显示

    [原创]禁止转载,需要转载请联系作者 想要使用Tecplot显示图1中这样一张云图,即删除掉接近0的一些正负的压力值,尝试了多种方法后成功实现: 图1 多截面云图显示 问题:如何去掉 -0.3< ...

  8. pagerank算法在数学模型中的运用(有向无环图中节点排序)

    一.模型介绍 pagerank算法主要是根据网页中被链接数用来给网页进行重要性排名. 1.1模型解释 模型核心: a. 如果多个网页指向某个网页A,则网页A的排名较高. b. 如果排名高A的网页指向某 ...

  9. 转 深入理解net core中的依赖注入、Singleton、Scoped、Transient

    出处:http://www.mamicode.com/info-detail-2200461.html 一.什么是依赖注入(Denpendency Injection) 这也是个老身常谈的问题,到底依 ...

  10. [LeetCode&Python] Problem 70. Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...