Multithreaded Programming (15)

Source: Internet
Author: User

The second parameter (starting time) for the next setwaitabletimer is specifically studied.

It has positive, negative, 0-valued three cases, preceded by a value of 0.

Learn about negative values (relative time), that is, how long it will take to start execution from the current count.

This relative time is 1/100 nanoseconds, for example, the assignment 3*10000000 is equivalent to 3 seconds.

1 s(秒) = 1,000       ms(毫秒);
1 s(秒) = 1,000,000     µs(微妙);
1 s(秒) = 1,000,000,000   ns(纳秒);
1 s(秒) = 1,000,000,000,000 ps(皮秒);

This example effect chart:

Code files:

Unit Unit1
Interface
uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms ,
Dialogs, Stdctrls;   
Type
TForm1 = Class (Tform)
Button1:tbutton;
Procedure Button1Click (Sender:tobject);
Procedure Formdestroy (sender:tobject);
End;  
Var
Form1:tform1
Implementation
{$R *.DFM}
var
f:integer;
Hwaitabletimer:thandle;
Function Mythreadfun (p:pointer): DWORD; stdcall;  
var
i,y:integer
Begin
Inc (f);
Y: = * f;
If WaitForSingleObject (Hwaitabletimer, INFINITE) = Wait_object_0 then
begin
For I: = 0 to 1000 do
B    Egin
Form1.Canvas.Lock;
Form1.Canvas.TextOut (M, Y, IntToStr (i));
Form1.Canvas.Unlock;
Sleep (1);
End;
End;
Result: = 0;
End;
Procedure Tform1.button1click (sender:tobject);  
var
Threadid:dword;
Duetime:int64;
Begin
Hwaitabletimer: = CreateWaitableTimer (Nil, True, nil);
Duetime: = -3*10000000; {3 seconds to execute}
SetWaitableTimer (Hwaitabletimer, duetime, 0, Nil, nil, False);
Repaint; F: = 0;
CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID);
CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID);
CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID);
End;
Procedure Tform1.formdestroy (sender:tobject);
Begin
CloseHandle (hwaitabletimer);
End;
End.

Form file:

object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 116
 ClientWidth = 179
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 OnDestroy = FormDestroy
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
  Left = 96
  Top = 83
  Width = 75
  Height = 25
  Caption = 'Button1'
  TabOrder = 0
  OnClick = Button1Click
 end
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.