delphi 最好的线程操作


本文整理自网络,侵删。

 unit Unit2;

interface

uses
Classes,SysUtils,Dialogs,Windows;

type
Ithreads = class(TThread)
private
{ Private declarations }
awer:integer;
protected
procedure Execute; override;
procedure Giveanswer;
end;

implementation

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure Ithreads.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ Ithreads }
uses Unit1;

procedure Ithreads.Giveanswer ;
begin
Form1.Edit1.Text := IntToStr(awer);
end;

procedure Ithreads.Execute;
var
i:integer;
begin
{ Place thread code here }
for i:= 1 to 500000 do
begin
inc(awer,round(abs(sin(sqrt(i)))));
synchronize(Giveanswer);
end;
end;

end.


=============================================

线程的创建、挂起、激活与终止

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
hThread:Thandle;//定义一个句柄
ThreadID:DWord;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function MyThreadFunc(P:pointer):Longint;stdcall;
var
i:longint;
DC:HDC;
S:string;
begin
DC:=GetDC(Form1.Handle);
for i:=0 to 500000 do begin
S:=Inttostr(i);
Textout(DC,10,10,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//创建线程,同时线程函数被调用
hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SuspendThread(hThread); //挂起线程
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ResumeThread(hThread); // 激活线程
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
TerminateThread(hThread,0); // 终止线程
end;

end.

相关阅读 >>

Delphi 如何让 tgpimage 直接从流中加载图片?

Delphi 网上获取北京时间 tinifile.readsection 方法在 android 下的应用及各种字符编码问题

Delphi writeln 写入一行文本

Delphi 安卓图像压缩bitmapcompress

Delphi getwindowhandle

Delphi读取android短信信息

Delphi 安装fastreport4.8.xx

Delphi 2010 fastmm 内存泄露使用方法

Delphi 获取unigui 控件id 及获取html值

Delphi 中的自动引用计数使用规则

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...