unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfMain = class(TForm)
    btnWaitForSingleObject: TButton;
    btnMsgWaitForMultipleObjects: TButton;
    procedure btnWaitForSingleObjectClick(Sender: TObject);
    procedure btnMsgWaitForMultipleObjectsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fMain : TfMain;
  PI    : TProcessInformation;
  SI    : TStartupInfo;

implementation

{$R *.dfm}

procedure TfMain.btnWaitForSingleObjectClick(Sender: TObject);
begin
  FillChar(SI, Sizeof(SI), #0);
  SI.cb := SizeOf(SI);
  if not CreateProcess('c:\windows\system32\notepad.exe', nil, nil, nil, false,
    NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
      MessageBox(Handle, '湾 忤轼腩 耱忸痂蜩 镳铞羼.', '', MB_OK or MB_ICONERROR);

  WaitForSingleObject(PI.hProcess, INFINITE);

  CloseHandle(PI.hProcess);
  CloseHandle(PI.hThread)
end;

procedure TfMain.btnMsgWaitForMultipleObjectsClick(Sender: TObject);
var  Res: DWORD;
     Msg: TMsg;
begin
  btnWaitForSingleObject.Enabled := false;
  btnMsgWaitForMultipleObjects.Enabled := false;

  FillChar(SI, Sizeof(SI), #0);
  SI.cb := SizeOf(SI);
  if not CreateProcess('c:\windows\system32\notepad.exe', nil, nil, nil, false,
    NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
      MessageBox(Handle, '湾 忤轼腩 耱忸痂蜩 镳铞羼.', '', MB_OK or MB_ICONERROR);

  repeat
    Res := MsgWaitForMultipleObjects(1, PI.hProcess, false, INFINITE, QS_PAINT);
    if Res = WAIT_OBJECT_0 + 1 then Application.ProcessMessages;
  until Res = WAIT_OBJECT_0;

  btnWaitForSingleObject.Enabled := true;
  btnMsgWaitForMultipleObjects.Enabled := true;

  CloseHandle(PI.hProcess);
  CloseHandle(PI.hThread)
end;

end.
