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之工作原理深入解析(上) ...
随机推荐
- 第十二周翻译-《Pro SQL Server Internals, 2nd edition》
<Pro SQL Server Internals, 2nd edition> 作者:Dmitri Korotkevitch 翻译:赖慧芳 译文: 专业SQL服务器内部 了解在引擎盖下发生 ...
- 移动端click事件无反应或反应慢 touchend事件页面滑动时频繁触发
H5页面的点击事件click 无论在浏览器 iframe还是小程序里面 都会出现点击无反应或者反应慢的情况出现 所以决定用touchend事件来代替click 但是touchend事件触发比较灵敏 在 ...
- C# 读取 appconfig文件配置数据库连接字符串,和配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connecti ...
- 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 20165326
恶意代码分析 实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systra ...
- 使用context关闭协程以及协程中的协程
package main import ( "sync" "context" "fmt" "time" ) var wg ...
- 认识Python 列表和元组
列表用方括号 l=[['xyz',123], 'abc', 3140000000.0, (23+1.3j), 'ddd'] 元组用圆括号 t=(['xyz', 123],'abc', 3140 ...
- java.lang.ClassNotFoundException: org.apache.storm.topology.IRichSpout
java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout at java.lang.Class.getDeclaredM ...
- JS查看对象属性的方式
var person = { type: 'person', say: function(){ console.log("Hellow World!") } } //以person ...
- c++ 集合的增删改查,与两集合的合并 缺陷(空间大小不灵活)
#if 1 #include <iostream> #include <stdlib.h> using namespace std; class List { public: ...
- 将lvm逻辑卷分出一部分
今天公司磁盘报警,查看磁盘发现挂载的/aa空间还很多,而/ee 空间不足,现将/aa上的lvm分出一部分空间.加到/ee上 注:首先要 df -T -h 开一下磁盘格式如果是xfs格式的lvm ...