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)
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
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(Self) then //如果为 True
begin
Color := clRed;
System.MonitorExit(Self); //也必须退出,否则 System.MonitorPulse(Self); 失效。
end;
end; procedure TForm1.Button2Click(Sender: TObject);
begin
System.MonitorPulse(Self);
end; procedure TForm1.FormCreate(Sender: TObject);
begin
TThread.CreateAnonymousThread(procedure
begin
while True do
begin
if Form1.CheckBox2.Checked then
Exit; System.MonitorEnter(Form1); //必须
Log('Thread1 Enter');
System.MonitorWait(Form1, INFINITE);
Log('Thread1 WaitFor');
System.MonitorExit(Form1); //必须
Log('Thread1 Exit'); Log(DateTimeToStr(Now));
TThread.Sleep();
end;
Log('Thread Exit');
end).Start;
end; end. 注意事项:
if System.MonitorTryEnter(Self) then //如果为 True
begin
Color := clRed;
System.MonitorExit(Self); //也必须退出(释放),否则 System.MonitorPulse(Self); 失效。
end;
System.MonitorTryEnter 与 System.MonitorEnter 的区别在于是否会 block 当前调用 System.MonitorEnter 或 System.MonitorTryEnter 的线程。 System.MonitorEnter 会 block 调用线程
System.MonitorTryEnter 不会 block 调用线程。 此外
System.MonitorEnter(Form1);
System.MonitorWait(Form1, INFINITE); //执行后, Button1的onclick事件中,System.MonitorTryEnter(Self) 总会返回 True
delphi 多线程之System.TMonitor (续一)的更多相关文章
- delphi 多线程之System.TMonitor
三天不写代码就手生! 把测试代码记录下来. unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, S ...
- 【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之工作原理深入解析(上) ...
随机推荐
- 初识Hibernate框架,进行简单的增删改查操作
Hibernate的优势 优秀的Java 持久化层解决方案 (DAO) 主流的对象—关系映射工具产品 简化了JDBC 繁琐的编码 将数据库的连接信息都存放在配置文件 自己的ORM框架 一定要手动实现 ...
- 设置ul的指定li 样式
设置ul的最后li 的样式 .custom-consumerIndex .card-content .list-block ul li:last-child .item-inner { border- ...
- selenium中下拉框的定位
from selenium import webdriverfrom selenium.webdriver.support.select import Selectimport timedriver ...
- 使用shell安装lnmp
1.简介 使用shell脚本安装lnmp,纯粹是偷懒,平时安装一些东西都写成脚本了,方便以后在其他机器安装的时候不用再去查找文档. PHP版本5.6.6 MYSQL版本5.6.26 NGINX版本1. ...
- Nim函数调用的几种形式
Nim函数调用的几种形式 Nim 转载条件:如果你需要转载本文,你需要做到完整转载本文所有的内容,不得删改文内的作者名字与链接.否则拒绝转载. 关于nim的例行介绍: Nim 是一门静态编译型的系统级 ...
- 关于position的一些问题
position属性: static:静止 relative:相对的 fixed:固定的 absolu:绝对的 position的一些实例子如下: HTML: <!DOCTYPE htm ...
- spring cloud 的安全连接
Spring Cloud可以增加HTTP Basic认证来增加服务连接的安全性. 1.pom.xml加入security启动器 <dependency> <!-- Spring Cl ...
- pycharm中字体大小的调整方法
一.file->settings->editor->font->size 二.file中键入mouse,在其下editor->general->mouse选中:ch ...
- Open Daylight integration with OpenStack: a tutorial
Open Daylight integration with OpenStack: a tutorial How to deploy OpenDaylight and integrate it wit ...
- [C# 基础知识系列]专题七: 泛型深入理解(一) (转载)
引言: 在上一个专题中介绍了C#2.0 中引入泛型的原因以及有了泛型后所带来的好处,然而上一专题相当于是介绍了泛型的一些基本知识的,对于泛型的性能为什么会比非泛型的性能高却没有给出理由,所以在这个专题 ...