多线程编程(4) - 从 CreateThread 说起[续二]
2021/4/30 12:55:16
本文主要是介绍多线程编程(4) - 从 CreateThread 说起[续二],对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
多线程编程(4) - 从 CreateThread 说起[续二]
function CreateThread( lpThreadAttributes: Pointer; dwStackSize: DWORD; lpStartAddress: TFNThreadStartRoutine; lpParameter: Pointer; {入口函数的参数} dwCreationFlags: DWORD; var lpThreadId: DWORD ): THandle; stdcall;
线程入口函数的参数是个无类型指针(Pointer), 用它可以指定任何数据; 本例是把鼠标点击窗体的坐标传递给线程的入口函数, 每次点击窗体都会创建一个线程.
运行效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} var pt: TPoint; {这个坐标点将会已指针的方式传递给线程, 它应该是全局的} function MyThreadFun(p: Pointer): Integer; stdcall; var i: Integer; pt2: TPoint; {因为指针参数给的点随时都在变, 需用线程的局部变量存起来} begin pt2 := PPoint(p)^; {转换} for i := 0 to 1000000 do begin with Form1.Canvas do begin Lock; TextOut(pt2.X, pt2.Y, IntToStr(i)); Unlock; end; end; Result := 0; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var ID: DWORD; begin pt := Point(X, Y); CreateThread(nil, 0, @MyThreadFun, @pt, 0, ID); {下面这种写法更好理解, 其实不必, 因为 PPoint 会自动转换为 Pointer 的} //CreateThread(nil, 0, @MyThreadFun, Pointer(@pt), 0, ID); end; end.
窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 128 ClientWidth = 229 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False onm ouseUp = FormMouseUp PixelsPerInch = 96 TextHeight = 13 end
这个例子还有不严谨的地方: 当一个线程 Lock 窗体的 Canvas 时, 其他线程在等待; 线程在等待时, 其中的计数也还在增加. 这也就是说: 现在并没有去处理线程的同步; 同步是多线程中最重要的课题, 快到了.
另外有个小技巧: 线程函数的参数是个 32 位(4个字节)的指针, 仅就本例来讲, 可以让它的 "高16位" 和 "低16位" 分别携带 X 和 Y; 这样就不需要哪个全局的 pt 变量了.
其实在 Windows 的消息中就是这样传递坐标的, 在 Windows 的消息中一般高字节是 Y、低字节是 X; 咱们这么来吧, 这样还可以使用给消息准备的一些方便的函数.
重写本例代码(当然运行效果和窗体文件都是一样的):
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); end; var Form1: TForm1; implementation {$R *.dfm} function MyThreadFun(p: Pointer): Integer; stdcall; var i: Integer; x,y: Word; begin x := LoWord(Integer(p)); y := HiWord(Integer(p)); {如果不使用 LoWord、HiWord 函数可以像下面这样: } //x := Integer(p); //y := Integer(p) shr 16; for i := 0 to 1000000 do begin with Form1.Canvas do begin Lock; TextOut(x, y, IntToStr(i)); Unlock; end; end; Result := 0; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var ID: DWORD; num: Integer; begin num := MakeLong(X, Y); {如果不使用 MekeLong、MakeWParam、MakeLParam、MakeResult 等函数, 可以像下面这样: } //num := Y shl 16 + X; CreateThread(nil, 0, @MyThreadFun, Ptr(num), 0, ID); {上面的 Ptr 是专门将一个数字转换为指针的函数, 当然也可以这样: } //CreateThread(nil, 0, @MyThreadFun, Pointer(num), 0, ID); end; end.
posted on 2009-02-11 10:42 万一 阅读(15770) 评论(25) 编辑 收藏
这篇关于多线程编程(4) - 从 CreateThread 说起[续二]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03如何安装 App 并连接到飞牛 NAS?-icode9专业技术文章分享
- 2024-10-03如何安装飞牛 TV 并连接到影视服务器?-icode9专业技术文章分享
- 2024-10-03如何在PVE和ESXI上安装飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS安装系统异常情况处理-icode9专业技术文章分享
- 2024-10-03飞牛NAS如何创建存储空间?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS硬盘会自动休眠吗?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何安装飞牛影视和创建媒体库?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何为家人朋友开通影视账号?-icode9专业技术文章分享