Multithreaded Programming (4)-From CreateThread [Cont.]

Source: Internet
Author: User

function CreateThread(
 lpThreadAttributes: Pointer;
 dwStackSize: DWORD;
 lpStartAddress: TFNThreadStartRoutine;
 lpParameter: Pointer; {入口函数的参数}
 dwCreationFlags: DWORD;
 var lpThreadId: DWORD
): THandle; stdcall;

The parameter of the thread entry function is an untyped pointer (pointer) that can be used to specify any data; This example passes the coordinates of the mouse-click form to the entry function of the thread, creating a thread each time the form is clicked.

Run Effect chart:

Code files: 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; {This coordinate point will be passed to the thread as a pointer, it should be global}
Function Mythreadfun (p:pointer): Integer; stdcall;  
var
I:integer;
    Pt2:tpoint; {Because the points given by the pointer parameters are changing at any time, you need to save the local variables of the thread}
Begin
Pt2: = PPoint (p) ^; Convert    
for I: = 0 to 1000000 does
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);
{This notation is better understood in fact, because PPoint is automatically converted to pointer}
//createthread (nil, 0, @MyThreadFun, pointer (@pt), 0, ID);
E nd
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.