delphi 多线程之System.TMonitor
三天不写代码就手生! 把测试代码记录下来。
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的更多相关文章
- delphi 多线程之System.TMonitor (续一)
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 【C#】线程之Parallel
在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...
- 多线程之join方法
join方法的功能就是使异步执行的线程变成同步执行.也就是说,当调用线程实例的start方法后,这个方法会立即返回,如果在调用start方法后后需要使用一个由这个线程计算得到的值,就必须使用join方 ...
- JAVA多线程之wait/notify
本文主要学习JAVA多线程中的 wait()方法 与 notify()/notifyAll()方法的用法. ①wait() 与 notify/notifyAll 方法必须在同步代码块中使用 ②wait ...
- JAVA多线程之volatile 与 synchronized 的比较
一,volatile关键字的可见性 要想理解volatile关键字,得先了解下JAVA的内存模型,Java内存模型的抽象示意图如下: 从图中可以看出: ①每个线程都有一个自己的本地内存空间--线程栈空 ...
- java多线程之yield,join,wait,sleep的区别
Java多线程之yield,join,wait,sleep的区别 Java多线程中,经常会遇到yield,join,wait和sleep方法.容易混淆他们的功能及作用.自己仔细研究了下,他们主要的区别 ...
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
WebAPI调用笔记 前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...
- Java基础-进程与线程之Thread类详解
Java基础-进程与线程之Thread类详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程与线程的区别 简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程 ...
- Android 多线程之IntentService 完全详解
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
随机推荐
- python的标准数据类型
python有5种标准的数据类型 1. number(数字) int(有符号的整形) long(长整[也可以代表八进制和16进制]) float(浮点型) complex(复数类型) 2.string ...
- CSS之box-sizing
盒子模型是CSS中很重要的概念,它涉及到的无非就是内容(content).填充(padding).边框(border).边界(margin).但是从上两张图可以看出,盒子模型其实有两种类型:标准盒模型 ...
- 通过SSH去连接 github 和bitbucket
github 和 bitbucket 都是项目托管服务器, 1 创建SSH private key and public key 首先需要安装git命令, 并且请检查是否有ssh 命令. 打开 Git ...
- IP通信基础原理课堂笔记----HCL(1)
PC端配置:打开接口,配置ip地址 交换机配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-ty ...
- redis cluster最简配置
redis cluster最简配置 master配置如下:(默认6379端口) bind 127.0.0.1 port 6379 timeout 0 databases 16 Master的redis ...
- Invoker-n颜色涂m个珠子的项链
参考https://blog.csdn.net/anxdada/article/details/76862564. https://blog.csdn.net/baidu_35643793/artic ...
- git教程:添加远程仓库
转自: 添加远程仓库 现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过 ...
- linux开机默认启动命令行模式
进入vi /etc/inittab 修改id:5:initdefault: 为id:3:initdefault:
- 个人项目cpu占用图
- 使用command line测试网速
wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py --no ...