资源描述:
《delphi多线程编程5》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、delphi多线程编程5窗体文件:objectForm1:TForm1 Left=0 Top=0 Caption='Form1' ClientHeight=206 ClientWidth=371 Color=clBtnFace Font.Charset=DEFAULT_CHARSET Font.Color=clWindowText Font.Height=-11 Font.Name='Tahoma' Font.Style=[] OldCreateOrder=False OnDestroy=FormDest
2、roy PixelsPerInch=96 TextHeight=13 objectPaintBox1:TPaintBox Left=8 Top=8 Width=114 Height=153 end objectPaintBox2:TPaintBox Left=128 Top=8 Width=114 Height=153 end objectPaintBox3:TPaintBox Left=248 Top=8 Width=114 Height=153 end objectButton
3、1:TButton Left=288 Top=172 Width=75 Height=25 Caption='Button1' TabOrder=0 OnClick=Button1Click endend 可以借助入口函数的参数,把这个程序简化一下(窗体和运行效果不变):unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,StdCtrls,Ex
4、tCtrls;type TForm1=class(TForm) PaintBox1:TPaintBox; PaintBox2:TPaintBox; PaintBox3:TPaintBox; Button1:TButton; procedureButton1Click(Sender:TObject); procedureFormDestroy(Sender:TObject); end;var Form1:TForm1;implementation{$R*.dfm}var h1,h2,h3:THa
5、ndle;{线程的入口函数:不同的线程画不同颜色的椭圆}functionThreadFun(p:Pointer):Integer;stdcall;var i,x1,y1,x2,y2:Integer; paint:TPaintBox;begin caseInteger(p)of 1:begin paint:=Form1.PaintBox1; paint.Canvas.Brush.Color:=clRed; end; 2:begin paint:=Form1.PaintBox2; pai
6、nt.Canvas.Brush.Color:=clGreen end; 3:begin paint:=Form1.PaintBox3; paint.Canvas.Brush.Color:=clBlue; end; end; fori:=0to5000dowithpaintdo begin x1:=Random(Width);y1:=Random(Height); x2:=Random(Width);y2:=Random(Height); Canvas.Lock; Canvas.Ell
7、ipse(x1,y1,x2,y2); Canvas.Unlock; Sleep(0); end; Result:=0;end;procedureTForm1.Button1Click(Sender:TObject);var ID:DWORD;begin h1:=CreateThread(nil,0,@ThreadFun,Ptr(1),0,ID); h2:=CreateThread(nil,0,@ThreadFun,Ptr(2),0,ID); h3:=CreateThread(nil,0,@Thread
8、Fun,Ptr(3),0,ID);end;procedureTForm1.FormDestroy(Sender:TObject);begin CloseHandle(h1); CloseHandle(h2); CloseHandle(h3);end;end. 多用点数组,再简化一下(窗体与效果一样):unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Class