Creation and use of Thread class

zhaozj2021-02-16  157

A, Thread class is created: unit Thread; {Thread class to create a build environment: Windows 2003 Sever Delphi 7.0 Enterprise} interfaceuses classes, sysutils, StdCtrls; type TB = class (TThread) private i: integer; Fedt: TEdit; procedure Update ; public procedure execute; override; constructor create (IsSuspended: Boolean; edt: TEdit); end; implementationuses MainForm; procedure TB.Update; begin Fedt.Text: = inttostr (i); end; constructor TB.create (IsSuspended: Boolean EDT: TEDIT; begin inherited create (Issuspend); fedt: = edt; end; procedure tb.execute; begin i: = 0; While (NOT TERMINATED) DO BEGIN SYNCHRONIZE (UPDATE); Inc (i); end; end two, Thread class using:. unit MainForm; interfaceuses Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Thread; type TfrmMain = class (TForm) Button1: TButton; Button2: TButton; edit1: tedit; edit2: tedit; procedure formcreate; procedure formclose (Sender: TpoBject; var Action: tclosection); Procedure Buttonc lick (Sender: TObject); private {Private declarations} public {Public declarations} end; var frmMain: TfrmMain; a, b: TB; implementation {$ R * .dfm} procedure TfrmMain.FormCreate (Sender: TObject); begin a : = TB.CREATE (TRUE, EDIT1); B: = Tb.create (True, Edit2); End; Procedure TfrmMain.FormClose (Sender: Tobject; VAR Action: TcloseAction); Begin FreeAndnil (a); FreeAndnil (b) End; procedure tfrmmain.buttonclick (sender: Tobject); var C: Tb; Begin if sender = Button1 THEN C: = a else c: = b; if c.suspended the beginning of c.Resume; CAPTION: = 'Pause'; end else begin c.suspend; (sender as tbutton) .caption: = 'start'; end; end; end.

转载请注明原文地址:https://www.9cbs.com/read-7883.html

New Post(0)