Synchronizing Threads and GUI

 
 
Code submitted by Jens Borrisholt

Multi-threading in Delphi lets you create applications that include several simultaneous paths of execution.

A "normal" Delphi application is single-threaded: all (VCL) objects access their properties and execute their methods within this single thread.

Threads and GUI

To speed up data processing in your application you can decide to include one or more "secondary" threads.

When several threads are running in the application a question arises: how to update your GUI (user interface) as a result of a thread execution. The question lies in the TThread class Synchronize method.

To update your application's user interface - main thread - from a secondary thread you need to call the Synchronize method - a thread-safe method that avoids multi-threading conflicts that can arise from accessing object properties or methods that are not thread-safe, or using resources not in the main thread of execution.

An example demo uses several buttons with progress bar, each progress bar displaying the current "state" of the thread execution...

Download source code

unit MainU;

 interface

 uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, ExtCtrls; type
//interceptor class
TButton = class(StdCtrls.TButton)
OwnedThread: TThread;
ProgressBar: TProgressBar;
end; TMyThread = class(TThread)
private
FCounter: Integer;
FCountTo: Integer;
FProgressBar: TProgressBar;
FOwnerButton: TButton; procedure DoProgress;
procedure SetCountTo(const Value: Integer) ;
procedure SetProgressBar(const Value: TProgressBar) ;
procedure SetOwnerButton(const Value: TButton) ;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean) ;
property CountTo: Integer read FCountTo write SetCountTo;
property ProgressBar: TProgressBar read FProgressBar write SetProgressBar;
property OwnerButton: TButton read FOwnerButton write SetOwnerButton;
end; TMainForm = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
Button2: TButton;
ProgressBar2: TProgressBar;
Button3: TButton;
ProgressBar3: TProgressBar;
Button4: TButton;
ProgressBar4: TProgressBar;
Button5: TButton;
ProgressBar5: TProgressBar;
procedure Button1Click(Sender: TObject) ;
end; var
MainForm: TMainForm; implementation {$R *.dfm} { TMyThread } constructor TMyThread.Create(CreateSuspended: Boolean) ;
begin
inherited;
FCounter := ;
FCountTo := MAXINT;
end; procedure TMyThread.DoProgress;
var
PctDone: Extended;
begin
PctDone := (FCounter / FCountTo) ;
FProgressBar.Position := Round(FProgressBar.Step * PctDone) ;
FOwnerButton.Caption := FormatFloat('0.00 %', PctDone * ) ;
end; procedure TMyThread.Execute;
const
Interval = ;
begin
FreeOnTerminate := True;
FProgressBar.Max := FCountTo div Interval;
FProgressBar.Step := FProgressBar.Max; while FCounter < FCountTo do
begin
if FCounter mod Interval = then Synchronize(DoProgress) ; Inc(FCounter) ;
end; FOwnerButton.Caption := 'Start';
FOwnerButton.OwnedThread := nil;
FProgressBar.Position := FProgressBar.Max;
end; procedure TMyThread.SetCountTo(const Value: Integer) ;
begin
FCountTo := Value;
end; procedure TMyThread.SetOwnerButton(const Value: TButton) ;
begin
FOwnerButton := Value;
end; procedure TMyThread.SetProgressBar(const Value: TProgressBar) ;
begin
FProgressBar := Value;
end; procedure TMainForm.Button1Click(Sender: TObject) ;
var
aButton: TButton;
aThread: TMyThread;
aProgressBar: TProgressBar;
begin
aButton := TButton(Sender) ;
if not Assigned(aButton.OwnedThread) then
begin
aThread := TMyThread.Create(True) ;
aButton.OwnedThread := aThread;
aProgressBar := TProgressBar(FindComponent(StringReplace(aButton.Name, 'Button', 'ProgressBar', []))) ;
aThread.ProgressBar := aProgressBar;
aThread.OwnerButton := aButton;
aThread.Resume;
aButton.Caption := 'Pause';
end
else
begin
if aButton.OwnedThread.Suspended then
aButton.OwnedThread.Resume
else
aButton.OwnedThread.Suspend; aButton.Caption := 'Run';
end;
end; end.

Synchronizing Threads and GUI in Delphi application的更多相关文章

  1. Delphi Application.MessageBox详解

    引数:1. Text:要显示的讯息2. Caption:讯息视窗的标题列文字3. Flags:讯息旗标     3.1. 可指定讯息视窗上的图示     3.2. 可指定讯息视窗出现的按钮     3 ...

  2. delphi application 和 form 主窗体 都是窗口

    application     也是一个窗体,  和一般窗体不一样. 主窗体 是一个窗体. 然后这两个窗体,分别计算pid   获得结果竟然一样. 另外句柄是动态,全部都不一样.每一次都不一样.

  3. 所有的GUI Toolkit,类型之多真开眼界

    The GUI Toolkit, Framework Page User interfaces occupy an important part of software development. Th ...

  4. Delphi中Messagedlg用法

    if MessageDlg('Welcome to my Delphi application. Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrY ...

  5. Delphi XE5教程5:程序的结构和语法

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  6. Delphi XE5教程4:程序和单元概述

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  7. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  8. Delphi TWebBrowser

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  9. MATLAB中文论坛帖子整理(GUI)

    MATLAB中文论坛帖子整理(GUI) 目   录  1.GUI新手之——教你读懂GUI的M文件... 10 2.GUI程序中改变current directory引起的问题... 15 3.GUI中 ...

随机推荐

  1. node学习笔记1——配置node环境变量及执行node文件

    最近在学习node,今天说一下node的变量环境配置.虽然网上有说,最新版的已经不需要配置这个东东了,但是我的电脑还是得配置.闲话少扯,进入正题: 1.安装node,这步就略过了.就是下载 node, ...

  2. 为已经存在的本地项目添加git,以及从远程仓库拉取代码并切换远程分支

    前提:先去gitlab或github网站上创建一个新项目,完毕后记得添加.ignore: 1.打开终端​,cd到已存在项目的目录 ​​​2.输入以下命令行,初始化一个本地仓库: ​git init 3 ...

  3. ps常用快捷键

    一. 二. 三.

  4. 关于Unity中混合模式、Alpha测试、深度测试、通道遮罩、面剔除的使用----渲染通道通用指令(二)

    混合模式 着色完成后,需要把颜色混合到帧缓冲区里面,涉及到源和目标. 1:在所有计算完成后,决定当前的计算结果输出到帧缓冲区时,如何混合源和目标,通常用来绘制半透明的物体;2: Blend Off 关 ...

  5. Opengl绘制我们的小屋(三)纹理绘制

    本准备先说光照相关实现,但是发现对那个模型实在看不下去了,于是先绘制纹理. 先看下基本纹理贴上去的显示效果.具体模型图请看上篇文章的实现,这篇只讲纹理实现. 我们常见的纹理绘制差不多如下,先写一个纹理 ...

  6. Android 8 声音调整过程

    记录Android 8声音调整过程. frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.j ...

  7. Android Error: This attribute must be localized.

    在android中使用mmm命令编译程序是出现错误. 这种问题一般情况是因为在res/xml文件夹下的中, 或者在res/layout下的文件中出现了没有多语言话的文本例. 解决方法: 不直接在布局文 ...

  8. SpringMVC系列(七)视图解析器和视图

    在springmvc.xml里面配置视图解析器 <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 --> <bean class="org ...

  9. nodejs基础 -- 多进程

    Node.js 多进程 我们都知道 Node.js 是以单线程的模式运行的,但它使用的是事件驱动来处理并发,这样有助于我们在多核 cpu 的系统上创建多个子进程,从而提高性能. 每个子进程总是带有三个 ...

  10. Github 提交本地代 到远程 菜鸟 简明教程

    方法1: 1. 右击项目文件夹  Git Bash Here 或   Git clone  Url  远程仓库地址  Directory  本地文件夹 2. 拷贝(新建)项目文件 3. 添加项目文件 ...