⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 多线程技术的简单实现
💻 ~PAS
字号:
unit unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, 

Dialogs,

StdCtrls;

type

TForm1 = class(TForm)

Edit1: TEdit;

class1button: TButton;

class2button: TButton ;

routines1Button: TButton;

routines2Button: TButton;
    Memo1: TMemo;

procedure class1buttonClick(Sender: TObject);

procedure routines1ButtonClick(Sender: TObject);

procedure routines2ButtonClick(Sender: TObject);

procedure class2buttonClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

tmythread=class(tthread)

count:integer;

myedit:tedit;

procedure show;virtual;abstract;

constructor create(myedit1:tedit);

end;

thread1=class(tmythread)

procedure show;override;

procedure execute;override;

end;

thread2=class(tmythread)

procedure show;override;

procedure execute;override;

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

procedure mythreadfunc;// 创建的线程函数

var

i:integer;

dc:hdc;

s:string;

begin

for i:=0 to 100000 do

begin

s:=inttostr(i);

dc:=getdc(form1.edit1.handle);

textout(dc,0,0,pchar(s),length(s));

releasedc(form1.edit1.handle,dc);

end;

end;

constructor tmythread.create(myedit1:tedit);// 创建线程

begin

inherited create(false);

myedit:=myedit1;

freeonterminate:=true; // 线程终止时自动删除对象,

end;

procedure thread1.show;// 类调用的线程函数

begin

myedit.Text:=inttostr(count);

end;

procedure thread1.execute;// 线程方法重载

var

i:integer;

begin

for i:=0 to 100000 do

begin

count:=i;

synchronize(show); // 线程调用同步

end;

end;

procedure thread2.show;// 类调用的线程函数

begin

mythreadfunc;

end;

procedure thread2.execute;// 线程方法重载

begin

synchronize(show); // 线程调用同步

end;

procedure TForm1.class1buttonClick(Sender: TObject);// 引用类方法创 建线程
begin

with thread1.create(edit1) do

end;

procedure TForm1.class2buttonClick(Sender: TObject); // 引用类方法创 建线程

begin

with thread2.create(edit1) do

end;

procedure TForm1.routines1ButtonClick(Sender: TObject); // 引用例程创建线程

var

hthread:thandle;

thid:dword;

begin

hthread:=beginthread(nil,0,@mythreadfunc,nil,0,thid);

if hthread=0 then

showmessage(' 创建线程失败');

end;

procedure TForm1.routines2ButtonClick(Sender: TObject);  // 用例程创建线程失败

begin

mythreadfunc;

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -