delphi多线程(3)
来源:互联网 发布:美工提升学历 编辑:程序博客网 时间:2024/05/16 07:55
看刘艺的《面向对象的编程思想》线程部分。做示例如下:
unit untThds;
interface
uses Classes,ExtCtrls,Graphics,untmain;
type TDrawThread = class(TThread)
FCanvas: TCanvas; // 这个地方封装的特别好,避免了代码的冗余。
FL,FT,FW,FH: integer;
x,y,z: integer;
protected procedure Drawing; virtual;
procedure Execute; override;
public constructor Create(Box:TPaintBox);// 构造函数中可以写入变量,利用它的canvas end; TCircleThread = class(TDrawThread)
public procedure Drawing; override;//利用的面向对象的多态。 end;
TRectangleThread = class(TDrawThread)
public procedure Drawing; override; end;
TOtherThread = class(TDrawThread)
public procedure Drawing;override; end;
implementation
constructor TDrawThread.Create(Box: TPaintBox);
begin
FL := Box.Left;
FT := Box.Top;
FW := Box.Width;
FH := Box.Height;
FCanvas := Box.Canvas;
FreeOnTerminate := True;//析构的时候直接关闭线程。
inherited Create(False); end;
procedure TDrawThread.Execute;
var i,j: Integer; begin
for i := 0 to 10 do begin
x := Round(Random(FW) + FL);
y := Round(Random(FH) + FT);
z := Round(Random(50));
Synchronize(Drawing);// 使用Delphi线程类中实现的异步,你也可以自己来使用FCanvas的Lock,unLock for j := 1 to 10000000 do// 主要进行延时。
z := Round(Sqr(x* Sqr(j)));
end;
end;
procedure TDrawThread.Drawing;
begin
FCanvas.Brush.Style := bsClear;
end; { TRectangleThread }
procedure TRectangleThread.Drawing;
begin
inherited Drawing;
FCanvas.Pen.Color := clRed;
FCanvas.Rectangle(x,y,(x + z),(y + z)); end;
{ TCircleThread }
procedure TCircleThread.Drawing; begin inherited Drawing;// 多么简洁的代码。 FCanvas.Pen.Color := clBlue; FCanvas.Ellipse(x,y,(x+z),(y+z)); end;
{ TOtherThread }
procedure TOtherThread.Drawing; begin inherited Drawing; FCanvas.Pen.Color := clBlack; FCanvas.Rectangle(x,y,(x+z),(y+z));
end;
end.
------------------------------------------------------------------------------------------------------------------------------------------
自己封装的一个临界区的类。
unit untCriticalSection;
interface
uses Windows;
type TMyCriticalSection = class(TObject) //封装使用临界区资源的类 private FMyCS: TRTLCriticalSection;
public constructor Create; destructor Destroy; override; procedure Lock; procedure UnLock; end;
implementation
constructor TMyCriticalSection.Create; begin InitializeCriticalSection(fmycs); // 初始化临界区 end;
destructor TMyCriticalSection.Destroy; begin DeleteCriticalSection(fmycs); //删除临界区 end;
procedure TMyCriticalSection.Lock; begin EnterCriticalSection(fmyCS); //进入临界区。 end;
procedure TMyCriticalSection.UnLock; begin LeaveCriticalSection(fmycs); //离开临界区 end;
end. 在使用的时候便于直接使用。
在后面的博客中将逐步接受多线程编程方面的知识
- delphi多线程(3)
- Delphi 多线程编程(3)
- delphi多线程
- delphi多线程
- delphi 多线程
- delphi多线程
- delphi多线程
- delphi 多线程
- Delphi多线程
- delphi多线程
- delphi多线程
- delphi多线程
- [Delphi]Delphi的多线程编程
- [Delphi]Delphi的多线程编程
- delphi多线程程序示例
- Delphi多线程测试
- Delphi多线程处理(转载)
- delphi的多线程编程
- js漂浮广告代码
- 谈c++ 和 c#关于字符串和其它类型的相加操作
- 今日から日本語で日記を書いてみましょう
- 重装Windows XP后无法启动linux怎么办?
- 有一些常识,可能你以后参加一些智力竞赛时会用的上。。
- delphi多线程(3)
- DIV CSS网页布局需要掌握的技巧汇总
- 安装基于Apache的Subversion服务器
- js 调整图片大小
- 重逢如走圈
- 如何用SQL写出当M*N时的螺旋矩阵算法
- Struts2+Hibernate3+Spring2中文问题
- PO/POJO/BO/DTO/VO
- 清空指定页面上所有的控件内容