首页 > 编程语言 > 详细

设计一个简单的多线程(Fecit)_1

时间:2020-03-27 15:04:30      阅读:60      评论:0      收藏:0      [点我收藏+]

 

D6高级编程,Fecit ,学习里面关于线程创建的一个例子.

技术分享图片

 

 

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;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(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

procedure MyThreadFunc;
  var
  i:integer;
  dc:hdc;
  s:string;
begin
    for i:=0 to 1000000 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 1000000 do
  begin
    count:=i;
    Synchronize(Show);
  end;
end;

procedure Thread2.show;
begin
  MyThreadFunc;
end;

procedure Thread2.Execute;
begin
  Synchronize(Show);
end;



{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Thread1.create(Edit1) do
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  with Thread2.create(Edit1) do
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  hthread:Thandle;
  thid:dword;
begin
  hthread:=Beginthread(nil,0,@MyThreadFunc,nil,0,thid);
  if hthread=0 then
     showmessage(failed);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  MyThreadFunc;
end;

end.


end.

 

设计一个简单的多线程(Fecit)_1

原文:https://www.cnblogs.com/CDPJ/p/12580942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!