资源描述:
《delphi多线程编程3》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、delphi多线程编程3窗体文件:objectForm1:TForm1 Left=0 Top=0 Caption='Form1' ClientHeight=140 ClientWidth=192 Color=clBtnFace Font.Charset=DEFAULT_CHARSET Font.Color=clWindowText Font.Height=-11 Font.Name='Tahoma' Font.Style=[] OldCreateOrder=False OnCreate=FormCreate PixelsPerInch=96 T
2、extHeight=13 objectButton1:TButton Left=109 Top=107 Width=75 Height=25 Caption='Button1' TabOrder=0 OnClick=Button1Click end objectEdit1:TEdit Left=109 Top=80 Width=75 Height=21 TabOrder=1 Text='Edit1' OnKeyPress=Edit1KeyPress endend 再用SyncObjs单元下的TSemaphore类实
3、现一次,使用方法差不多,运行效果也一样:unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,StdCtrls;type TForm1=class(TForm) Button1:TButton; Edit1:TEdit; procedureButton1Click(Sender:TObject); procedureFormCreate(Sender:TObject); procedureF
4、ormDestroy(Sender:TObject); procedureEdit1KeyPress(Sender:TObject;varKey:Char); end;var Form1:TForm1;implementation{$R*.dfm}usesSyncObjs;var f:Integer; MySemaphore:TSemaphore;functionMyThreadFun(p:Pointer):DWORD;stdcall;var i,y:Integer;begin Inc(f); y:=20*f; ifMySemaphore.W
5、aitFor(INFINITE)=wrSignaledthen begin fori:=0to1000do begin Form1.Canvas.Lock; Form1.Canvas.TextOut(20,y,IntToStr(i)); Form1.Canvas.Unlock; Sleep(1); end; end; MySemaphore.Release; Result:=0;end;procedureTForm1.Button1Click(Sender:TObject);var ThreadID:DWORD;begin
6、 ifAssigned(MySemaphore)thenMySemaphore.Free; MySemaphore:=TSemaphore.Create(nil,StrToInt(Edit1.Text),5,''); Self.Repaint; f:=0; CreateThread(nil,0,@MyThreadFun,nil,0,ThreadID); CreateThread(nil,0,@MyThreadFun,nil,0,ThreadID); CreateThread(nil,0,@MyThreadFun,nil,0,ThreadID);
7、 CreateThread(nil,0,@MyThreadFun,nil,0,ThreadID); CreateThread(nil,0,@MyThreadFun,nil,0,ThreadID);end;{让Edit只接受12345五个数}procedureTForm1.Edit1KeyPress(Sender:TObject;varKey:Char);begin ifnotCharInSet(Key,['1'..'5'])thenKey:=#0;end;procedureTForm1.FormCreate(Sender:TObject);be
8、gin Edit1.Text:='1';end;procedureTForm1.FormDestroy(Sender:TObject);begin i