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之工作原理深入解析(上) ...
随机推荐
- 如何优雅的关闭Golang Channel?
Channel关闭原则 不要在消费端关闭channel,不要在有多个并行的生产者时对channel执行关闭操作. 也就是说应该只在[唯一的或者最后唯一剩下]的生产者协程中关闭channel,来通知消费 ...
- JAVA作业三
(一)学习总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Gr ...
- 《贝贝GO》服务条款
服务条款 一.服务条款的确认与接收 1.贝贝GO客户端软件(以下简称“本软件”)各项电子服务的所有权和运作权归属于“东莞市山水信息技术有限公司”(以下称“本公司”)所有,本软件提供的服务将完全按照其发 ...
- php中生成透明背景png缩略图程序
/** *$sourePic:原图路径 * $smallFileName:小图名称 * $width:小图宽 * $heigh:小图高 */function pngthumb($sourePic,$s ...
- Linux 驱动——Button驱动1
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/i ...
- MySQL数据库“十宗罪”【十大经典错误案例】
原文作者:张甦 来源:http://blog.51cto.com/sumongodb 今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数 ...
- oracle中left join,right join,inner join的坑
本文主要是记录一下实际使用oracle中join查询遇到的坑 1.用到两张表,学生表和学年分数表,先建立 2.普通连接查询 INNER JOIN,查询每个学年有成绩的学生以及分数情况 LFET JOI ...
- 项目启动失败,异常代码(StandardEngine[Catalina].StandardHost[localhost].StandardContext[/credit]]) ,dataSource 也报错
问题:tomcat 项目启动失败(有多个springboot项目)! 28-Apr-2019 12:01:12.162 严重 [localhost-startStop-1] org.apache.ca ...
- 加载xib文件,如果想在初始化的时候就添加点东西就重载-(id)initWithCoder:(NSCoder *)aDecoder
- (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { self.cl ...
- 微信小程序生成二维码工具
实现的效果 使用(非自适应) 完整代码请参考网址里https://github.com/tomfriwel/weapp-qrcode 页面wxml中放置绘制二维码的canvas: <canvas ...