How does delphi wait for the child process to end?

Source: Internet
Author: User

The job objects Kernel Object of Windows 2000 is required to wait for the end of the process tree. By default, it is not defined in Delphi windows pas. Therefore, you must manually reference the job-related definition. Next I will set jobs. PAAs

Unit jobs;

{
Interface Unit for Windows 2000 job objects.

Windows job object definition James
}

Interface

Uses windows;

Type
Tjobobjectinfoclass = Cardinal;

Pjobobjectassociatecompletionport = ^ tjobobjectassociatecompletionport;
Tjobobjectassociatecompletionport = record
Completionkey: pointer;
Completionport: thandle;
End;

Pjobobjectbasiclimitinformation = ^ tjobobjectbasiclimitinformation;
Tjobobjectbasiclimitinformation = record
Perprocessusertimelimit: tlargeinteger;
Perjobusertimelimit: tlargeinteger;
Limitflags: DWORD;
Minimumworkingsetsize: DWORD;
Maximumworkingsetsize: DWORD;
Activeprocesslimit: DWORD;
Affinity: DWORD;
Priorityclass: DWORD;
Schedulingclass: DWORD;
End;

Pjobobjectbasicuirestrictions = ^ tjobobjectbasicuirestrictions;
Tjobobjectbasicuirestrictions = record
Uirestrictionsclass: DWORD;
End;

Pjobobjectendofjobtimeinformation = ^ tjobobjectendofjobtimeinformation;
Tjobobjectendofjobtimeinformation = record
Endofjobtimeaction: DWORD;
End;

Tiocounters = record {all fields shocould be actually unsigned int64's}
Readoperationcount: int64;
Writeoperationcount: int64;
Otheroperationcount: int64;
Readtransfercount: int64;
Writetransfercount: int64;
Othertransfercount: int64;
End;

Pjobobjectextendedlimitinformation = ^ tjobobjectextendedlimitinformation;
Tjobobjectextendedlimitinformation = record
Basiclimitinformation: tjobobjectbasiclimitinformation;
Ioinfo: tiocounters;
Processmemorylimit: DWORD;
Jobmemorylimit: DWORD;
Peakprocessmemoryused: DWORD;
Peakjobmemoryused: DWORD;
End;

Pjobobjectsecuritylimitinformation = ^ tjobobjectsecuritylimitinformation;
Tjobobjectsecuritylimitinformation = record
Securitylimitflags: DWORD;
Jobtoken: thandle;
Sidstodisable: ptokengroups;
Privilegestodelete: ptokenprivileges;
Restrictedsids: ptokengroups;
End;

Pjobobjectbasicaccountinginformation = ^ tjobobjectbasicaccountinginformation;
Tjobobjectbasicaccountinginformation = record
Totalusertime: tlargeinteger;
Totalkerneltime: tlargeinteger;
Thisperiodtotalusertime: tlargeinteger;
Thisperiodtotalkerneltime: tlargeinteger;
Totalpagefaultcount: DWORD;
Totalprocesses: DWORD;
Activeprocesses: DWORD;
Totalterminatedprocesses: DWORD;
End;

Pjobobjectbasicandioaccountinginformation = ^ tjobobjectbasicandioaccountinginformation;
Tjobobjectbasicandioaccountinginformation = record
Basicinfo: tjobobjectbasicaccountinginformation;
Ioinfo: tiocounters;
End;

Pjobobjectbasicprocessidlist = ^ tjobobjectbasicprocessidlist;
Tjobobjectbasicprocessidlist = record
Numberofassignedprocesses: DWORD;
Numberofprocessidsinlist: DWORD;
Processidlist: array [0 .. 0] of ulong;
End;

Const
{For tjobobjectinfoclass}
Jobobjectbasicaccountinginformation = 1;
Jobobjectbasiclimitinformation = 2;
Jobobjectbasicprocessidlist = 3;
Jobobjectbasicuirestrictions = 4;
Jobobjectsecuritylimitinformation = 5;
Jobobjectendofjobtimeinformation = 6;
Jobobjectassociatecompletionportinformation = 7;
Jobobjectbasicandioaccountinginformation = 8;
Jobobjectextendedlimitinformation = 9;
Maxjobobjectinfoclass = 10;

{Miscellaneous constants}
Job_object_assign_process = $0001;
Job_object_set_attributes = $0002;
Job_object_query = $0004;
Job_object_terminate = $0008;
Job_object_set_security_attributes = $0010;
Job_object_all_access = standard_rights_required or
Synchronize or $ 1f;
Job_object_terminate_at_end_of_job = 0;
Job_object_post_at_end_of_job = 1;
Job_object_msg_end_of_job_time = 1;
Job_object_msg_end_of_process_time = 2;
Job_object_msg_active_process_limit = 3;
Job_object_msg_active_process_zero = 4;
Job_object_msg_new_process = 6;
Job_object_msg_exit_process = 7;
Job_object_msg_abnormal_exit_process = 8;
Job_object_msg_process_memory_limit = 9;
Job_object_msg_job_memory_limit = 10;
Job_object_limit_workingset =00000001;
Job_object_limit_process_time = $00000002;
Job_object_limit_job_time = $00000004;
Job_object_limit_active_process = $00000008;
Job_object_limit_affinity = $00000010;
Job_object_limit_priority_class =00000020;
Job_object_limit_preserve_job_time = $00000040;
Job_object_limit_scheduling_class =00000080;
Job_object_limit_process_memory =00000100;
Job_object_limit_job_memory =00000200;
Job_object_limit_die_on_unhandled_exception = $00000400;
Job_object_limit_breakaway_ OK = $00000800;
Job_object_limit_silent_breakaway_ OK = $00001000;
Job_object_limit_reserved1 = $00002000;
Job_object_limit_reserved2 = $00004000;
Job_object_limit_reserved3 = $00008000;
Job_object_limit_reserved4 = $00010000;
Job_object_limit_reserved5 = $00020000;
Job_object_limit_reserved6 = $00040000;
Job_object_limit_valid_flags =0007 FFFF;
Job_object_basic_limit_valid_flags = $ 000000ff;
Job_object_extended_limit_valid_flags = $ 1_1fff;
Job_object_reserved_limit_valid_flags = $0007 FFFF;
Job_object_uilimit_none = $00000000;
Job_object_uilimit_handles =00000001;
Job_object_uilimit_readclipboard = $00000002;
Job_object_uilimit_writequalboard = $00000004;
Job_object_uilimit_systemparameters = $00000008;
Job_object_uilimit_displaysettings =00000010;
Job_object_uilimit_globalatoms = $00000020;
Job_object_uilimit_desktop = $00000040;
Job_object_uilimit_exitwindows = $00000080;
Job_object_uilimit_all = $ 000000ff;
Job_object_ui_valid_flags = $ 000000ff;
Job_object_security_no_admin =00000001;
Job_object_security_restricted_token =$ 00000002;
Job_object_security_only_token = $00000004;
Job_object_security_filter_tokens =$ 00000008;
Job_object_security_valid_flags = $ 0000000f;

// Create_breakaway_from_job = $01000000;

Function assignprocesstojobobject (hjob, hprocess: thandle): bool; stdcall;
External Kernel32 name 'assignprocesstojobobject ';

Function createjobobject (lpjobattributes: psecurityattributes;
Lpname: pansichar): thandle; stdcall;
External Kernel32 name 'createjobobjecta ';

Function openjobobject (dwdesiredaccess: DWORD; binherithandle: bool;
Lpname: pansichar): thandle; stdcall;
External Kernel32 name 'openjobobjecta ';

Function queryinformationjobobject (hjob: thandle;
Jobobjectinformationclass: tjobobjectinfoclass;
Lpjobobjectinformation: pointer;
Cbjobobjectinformationlength: DWORD;
Lpreturnlength: pdword): bool; stdcall;
External Kernel32 name 'queryionionjobobject ';

Function setinformationjobobject (hjob: thandle;
Jobobjectinformationclass: tjobobjectinfoclass;
Lpjobobjectinformation: pointer;
Cbjobobjectinformationlength: DWORD): bool; stdcall;
External Kernel32 name 'setionionjobobject ';

Function terminatejobobject (hjob: thandle; uexitcode: uint): bool; stdcall;
External Kernel32 name 'terminatejobobject ';

Implementation

End.

--- The following is the implementation code. I dedicated a thread to start the process. In this way, the external only needs to call the thread directly and bind the thread to two events -----------------------------------------

Unit waitprocesstree;

Interface
Uses
Windows, classes;
Type
// Start running
Tstartprocessevent = procedure (Sender: tobject; command: string) of object;
// Fast running
Tendprocessevent = procedure (Sender: tobject; command: string) of object;
Type
Twaitprocesstree = Class (tthread)
Private
Fstartprocessevent: tstartprocessevent;
Fendprocessevent: tendprocessevent;
Fcommand: string;
Protected
Procedure waitprocess; virtual;
Function executeprocess (exe: string): thandle;
Procedure execute; override;
Public
Constructor create (createsuincluded: Boolean; command: string );
Destructor destroy; override;
Property onstartprocess: tstartprocessevent read fstartprocessevent write fstartprocessevent;
Property onendprocess: tendprocessevent read fendprocessevent write fendprocessevent;
End;
Implementation
Uses
Jobs;
{Twaitprocesstree}

Constructor twaitprocesstree. Create (createsuincluded: Boolean; command: string );
Begin
Inherited create (createsuincluded );
Self. freeonterminate: = true;
Fcommand: = command;

End;

Destructor twaitprocesstree. Destroy;
Begin

Inherited;
End;

Procedure twaitprocesstree. Execute;
Begin
Inherited;
If assigned (fstartprocessevent) then
Fstartprocessevent (self, fcommand );
Self. waitprocess;
End;

Function twaitprocesstree. executeprocess (exe: string): thandle;
VaR
Si: tstartupinfo;
Pi: tprocessinformation;

Begin
Result: = invalid_handle_value;
Fillchar (Si, sizeof (SI), 0 );
Si. CB: = sizeof (SI );
If CreateProcess (nil, pchar (exe), nil, nil, false, 0, nil, nil, Si, Pi) then begin
{Close thread handle}
Closehandle (PI. hthread );
Result: = pi. hprocess;
End
Else exit;
End;

Procedure twaitprocesstree. waitprocess;
VaR
Job, Pro: thandle;
Prolist: pjobobjectbasicprocessidlist;
// Joblimit: pjobobjectbasiclimitinformation;
Len: pdword;
Begin
// Create a job object
Job: = createjobobject (nil, nil );
Try

New (prolist );
// New (joblimit );
// Joblimit ^. limitflags: = jobs. job_object_limit_breakaway_ OK;
// Execute the process
Pro: = self. executeprocess (fcommand );
// If setinformationjobobject (job, 2, joblimit, sizeof (tjobobjectbasiclimitinformation) then
// Begin
If assignprocesstojobobject (job, Pro) then
Begin
While true do
Begin
Queryinformationjobobject (job, 3, prolist, sizeof (tjobobjectbasicprocessidlist), Len );
If prolist ^. numberofprocessidsinlist <= 0 then
Break;

Sleep (1000 );
End;
If assigned (fendprocessevent) then
Fendprocessevent (self, fcommand );
End;
// End;
Finally
// Jobs. terminatejobobject (job, 0 );
Closehandle (job );
End;
End;
End.

---------------- The following is the external call thread method ---------------------------------------------------------------

Procedure tform1.button6click (Sender: tobject );
VaR
TH: twaitprocesstree;
Begin
TH: = twaitprocesstree. Create (true, edit1.text );
Th. onstartprocess: = self. startprocess;
Th. onendprocess: = self. endprocess;
Th. Resume;

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.