Multi-thread programming (17)-waitabletimer for multi-thread synchronization (waiting for timer objects) [continued 3]

Source: Internet
Author: User
Tags apc
According to the main function of waitabletimer, it is a bit inappropriate to discuss it in the thread synchronization topic. It is about to end.

 
// Check the APC callback function Format again: Procedure timerapcproc (lpargtocompletionroutine: pointer; dwtimerlowvalue, dwtimerhighvalue: DWORD); stdcall;

  

The last two parameters of timerapcproc are actually passing a value. They must be combined into a tfiletime type for use.
This time is the time when the APC function is called. Let's take a look at the above example with a slight modification:


Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); Procedure formdestroy (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var htimer: thandle; {APC function} procedure merge (vertex: pointer; dwtimerlowvalue: DWORD; dwtimerhighvalue: DWORD); stdcall; var utcfiletime, localfiletime: tfiletime; systemtime: tsystemtime; datetime: tdatetime; begin {combine dwtimerlowvalue with dwtimerhighvalue into a time in tfiletime format} utcfiletime. dwlowdatetime: = dwtimerlowvalue; utcfiletime. dwhighdatetime: = dwtimerhighvalue; filetimetolocalfiletime (utcfiletime, localfiletime); {from world standard timing to local time} filetimetosystemtime (localfiletime, systemtime); {to System Format time} datetime: = systemtimetodatetime (systemtime); {convert to tdatetime} form1.text: = datetimetostr (datetime); sleepex (infinite, true); end; {thread entry function} function mythreadfun (P: pointer): integer; stdcall; var duetime: int64; begin duetime: = 0; If setwaitabletimer (htimer, duetime, 1000, @ timerapcproc, nil, false) then begin sleepex (infinite, true); end; Result: = 0; end; Procedure tform1.button1click (Sender: tobject); var ID: DWORD; begin if htimer = 0 then htimer: = createwaitabletimer (nil, true, nil); createthread (nil, 0, @ mythreadfun, nil, 0, ID); end; Procedure tform1.button2click (Sender: tobject); begin cancelwaitabletimer (htimer); end; procedure tform1.formdestroy (Sender: tobject); begin closehandle (htimer); end.
 
   
 

Form file:

 
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 86 clientwidth = 256 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false pixelsperinch = 96 textheight = 13 object button1: tbutton left = 23 Top = 32 width = 97 Height = 25 caption = #21551 #21160 #23450 #26102 #22120 taborder = 0 onclick = button1click end object button2: tbutton left = 134 Top = 32 width = 97 Height = 25 caption = #21462 #28040 #23450 #26102 #22120 taborder = 1 onclick = button2click endend

  

The pointer parameter next to the callback function in setwaitabletimer will be passed to the first parameter of the APC function;
As a pointer, it can carry any data. Here, it carries a coordinate point (the place where the mouse clicks the form), for example:


Code file:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) Procedure formdestroy (Sender: tobject ); procedure formmousedown (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); end; var form1: tform1; implementation {$ R *. DFM} var htimer: thandle; Pt: tpoint; {APC function} procedure merge (vertex: pointer; dwtimerlowvalue: DWORD; dwtimerhighvalue: DWORD); stdcall; var utcfiletime, localfiletime: tfiletime; systemtime: tsystemtime; datetime: tdatetime; pt2: tpoint; begin utcfiletime. dwlowdatetime: = dwtimerlowvalue; utcfiletime. dwhighdatetime: = dwtimerhighvalue; filetimetolocalfiletime (utcfiletime, localfiletime); filetimetosystemtime (localfiletime, systemtime); datetime: = systemtimetodatetime (systemtime); pt2: = Ppoint; {accept pointer parameter} form1.canvas. lock; form1.canvas. textout (pt2.x, pt2.y, datetimetostr (datetime); form1.canvas. unlock; sleepex (infinite, true); end; {thread entry function} function mythreadfun (P: pointer): integer; stdcall; var duetime: int64; begin duetime: = 0; {parameter @ PT here is the pointer to the coordinate structure when the mouse clicks the form, it will be passed to the first parameter of the APC function} If setwaitabletimer (htimer, duetime, 1000, @ timerapcproc, @ PT, false) then begin sleepex (infinite, true); end; Result: = 0; end; {create a waitabletimer object and thread} procedure tform1.formmousedown (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); var ID: DWORD; begin PT: = point (x, y ); {coordinate point assignment here} If htimer = 0 then htimer: = createwaitabletimer (nil, true, nil); createthread (nil, 0, @ mythreadfun, nil, 0, ID); end; Procedure tform1.formdestroy (Sender: tobject); begin closehandle (htimer); end.
 
   
 

Form file:

 
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 135 clientwidth = 195 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false onmousedown = formmousedown pixelsperinch = 96 textheight = 13end

  

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.