Delphi multithreaded Programming (9)--Understanding wait function WaitForSingleObject

Source: Internet
Author: User

Reprinted from: In Case of blog

Jump to wait for the function WaitForSingleObject, because the following mutex, Semaphore, Event, Waitabletimer and other synchronization means to use this function. But the wait function can be more than waitforsingleobject this one, but it is the simplest

function WaitForSingleObject (    hhandle:thandle;    The handle of the object to wait    Dwmillseconds:dword;    Wait time, in milliseconds): DWORD; Srdcall;        

The return value is described below

Wait_object_0      {Waiting, in this case: the process finally ended}wait_timeout       {wait for the point (you specify the time), also did not wait for}wait_abandoned  {finally wait for, But they still don't let me do it; this is generally a mutex}

  The second parameter of the WaitForSingleObject is usually INFINITE to the constant value, which means that it is death.

What is WaitForSingleObject waiting for? In multithreading is waiting for the end of another thread to execute its own code, but he can wait for more than one thread; Here's an example that waits for the end of another process to run

The code is as follows

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;ty        PE tform1=class (tform) Button1:tbutton;    Procedure Button1Click (Sender:tobject);    End;var form1:tform1;implementation{$R *.dfm}var hprocess:thandle; {Process Handle} {The process that waits for a specified handle ends}function mythreadfun (p:pointer);D word;stdcall;begin if WaitForSingleObject (hprocess, INFINITE) =    Wait_object_0 then form1.text:= Format (' process%d closed ', [hprocess]);    result:= 0;end;procedure Tform1.button1click (sender:tobject); var pinfo:tprocessinformation;    Sinfo:tstartupinfo;    Path:array[0..max_path-1] of Char;    Threadid:dword;begin {First get Notepad path} getsystemdirectory (path, MAX_PATH);    StrCat (Path, ' \notepad.exe ');    {Open Notepad with CreateProcess and get the process handle, and then establish thread monitoring}    Fillchar (Sinfo, SizeOf (Sinfo), 0);    If CreateProcess (Path, nil, nil, nil, False, 0, Nil, nil, sinfo, pInfo) THEN begin hprocess:= pinfo.hprocess; Gets the process handle text:= Format (' Process%d started ', [hprocess]);    CreateThread (nil, 0, @MyThreadFun, nil, 0, ThreadID); Establish thread monitoring end;end;end.

Form files

Object Form1:tform1 Left  = 0  Top = 0  Caption = ' Form1 '  clientheight = 124  clientwidth = 241  Color = Clbtnface  font.charset = default_charset  font.color = clwindowtext  font.height = -11  font.name = ' Tahoma '  font.style = []  oldcreateorder = False  pixelsperinch = TextHeight = All  Object Button1:tbutton Left    =    Top =    Width =    Height = Caption =    ' Button1 '    taborder = 0< C21/>onclick = Button1Click  endend

  

Delphi multithreaded Programming (9)--Understanding wait function WaitForSingleObject

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.