1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
24 #include <MUtils/JobObject.h>
31 #define WIN32_LEAN_AND_MEAN 1
39 class JobObject_Private
41 friend class JobObject
;
44 JobObject_Private(void)
53 MUtils::JobObject::JobObject(void)
55 p(new JobObject_Private())
57 const HANDLE jobObject
= CreateJobObject(NULL
, NULL
);
58 if((jobObject
!= NULL
) && (jobObject
!= INVALID_HANDLE_VALUE
))
60 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo
;
61 memset(&jobExtendedLimitInfo
, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION
));
62 memset(&jobExtendedLimitInfo
.BasicLimitInformation
, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION
));
63 jobExtendedLimitInfo
.BasicLimitInformation
.LimitFlags
= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
| JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION
;
64 if(SetInformationJobObject(jobObject
, JobObjectExtendedLimitInformation
, &jobExtendedLimitInfo
, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION
)))
66 p
->m_hJobObject
= jobObject
;
70 qWarning("Failed to set job object information!");
71 CloseHandle(jobObject
);
76 qWarning("Failed to create the job object!");
80 MUtils::JobObject::~JobObject(void)
84 CloseHandle(p
->m_hJobObject
);
85 p
->m_hJobObject
= NULL
;
91 bool MUtils::JobObject::addProcessToJob(const QProcess
*proc
)
95 qWarning("Cannot assign process to job: No job bject available!");
99 if(Q_PID pid
= proc
->pid())
102 if(!GetExitCodeProcess(pid
->hProcess
, &exitCode
))
104 qWarning("Cannot assign process to job: Failed to query process status!");
108 if(exitCode
!= STILL_ACTIVE
)
110 qWarning("Cannot assign process to job: Process is not running anymore!");
114 if(!AssignProcessToJobObject(p
->m_hJobObject
, pid
->hProcess
))
116 qWarning("Failed to assign process to job object!");
124 qWarning("Cannot assign process to job: Process handle not available!");
129 bool MUtils::JobObject::terminateJob(unsigned int exitCode
)
133 if(TerminateJobObject(p
->m_hJobObject
, exitCode
))
139 qWarning("Failed to terminate job object!");
145 qWarning("Cannot assign process to job: No job bject available!");