• TThread.CreateAnonymousThread() 匿名线程对象的应用


    unit Unit1;

    interface

    uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,System.SyncObjs;

    type
    TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    private
    { Private declarations }
    finish: Boolean;
    breaking: Boolean;
    public
    procedure Myfun;
    end;

    var
    Form1: TForm1;
    readwritelock: TCriticalSection;

    implementation

    {$R *.dfm}

    procedure TForm1.btn1Click(Sender: TObject);
    begin
    finish := False;
    breaking := False;
    TThread.CreateAnonymousThread(Myfun).Start;
    end;

    procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
    readwritelock.Enter;
    try
    breaking := True;
    finally
    readwritelock.Leave;
    end;
    while not finish do
    Sleep(10);
    end;

    procedure TForm1.Myfun;
    var
    i : Integer;
    b: Boolean;
    begin
    for i := 0 to 5000000 do
    begin
    readwritelock.Enter;
    try
    b := breaking;
    finally
    readwritelock.Leave;
    end;
    if b then Break;

    Self.Canvas.Lock;
    Canvas.TextOut(10,10,IntToStr(i));
    Self.Canvas.Unlock;
    end;
    finish := True;
    end;

    initialization
    readwritelock := TCriticalSection.Create;
    end.

    用加法的方式去爱人, 用减法的方式去怨恨, 用乘法的方式去感恩。 人生,最重要的不是得失, 而是拥有一颗善待自己的平常心!
  • 相关阅读:
    springboot + driud连接池踩的坑____新手学习
    tomcat的安装
    无限极分类
    javascript ECMAscript 和node.js commonJs之间的关系
    变量名,引用和地址
    java中闭包的理解
    thinkphp 模型的curd
    thinkphp之migration 迁移文件的使用
    验证ArrayList是线程不安全的集合
    一个java小程序,盗取插入的U盘中的数据。
  • 原文地址:https://www.cnblogs.com/sunday-night/p/6203173.html
Copyright © 2020-2023  润新知