{"id":2284,"date":"2013-06-06T20:31:43","date_gmt":"2013-06-06T11:39:37","guid":{"rendered":"https:\/\/programresource.net\/?p=2284"},"modified":"2013-06-06T21:06:17","modified_gmt":"2013-06-06T12:06:17","slug":"2284","status":"publish","type":"post","link":"https:\/\/programresource.net\/en\/2013\/06\/06\/2284.html","title":{"rendered":"How to wait for process termination when parent process terminates before child process"},"content":{"rendered":"<p>If you want to call external process and want to wait for it to terminate, you can call<a href=\"https:\/\/programresource.net\/en\/2013\/05\/30\/2278.html\"> CreateProcess function and wait using WaitForSingeObject API<\/a>.<\/p>\n<p>However, some process just launches child process and terminates immediately, resulting application still running but process terminates and returns to code.<\/p>\n<p>Below is sample code you can use in such case, where it monitors for process and child processes, and wait for all family process to terminate.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">BOOL RunandWait(char *runfname,DWORD *dwretval)\r\n{\r\n\t\/\/Run and track process and child process, wait for all family process to terminate\r\n\tSTARTUPINFO si;\r\n\tPROCESS_INFORMATION pi;\r\n\tDWORD i;\r\n\tHANDLE hProcess;\r\n\tBOOL bJobAllEnd;\r\n\r\n\tsi.cb = sizeof(si);\r\n\tsi.lpReserved = NULL;\r\n\tsi.lpDesktop = NULL;\r\n\tsi.lpTitle = NULL;\r\n\tsi.dwFlags = 0;\r\n\tsi.cbReserved2 = 0;\r\n\tsi.lpReserved2 = NULL;\r\n\r\n\tif (!CreateProcess(NULL,runfname,NULL,NULL,FALSE,0,NULL,NULL,&amp;si,&amp;pi))\r\n\t\treturn FALSE;\r\n\r\n\tPJOBOBJECT_BASIC_PROCESS_ID_LIST pList;\r\n\tpList = (PJOBOBJECT_BASIC_PROCESS_ID_LIST)GlobalAlloc(GMEM_FIXED, 10000);\r\n\r\n\tHANDLE hJob;\r\n\thJob = CreateJobObject(NULL, &amp;quot;Paging Job Object&amp;quot;);\r\n\r\n\tAssignProcessToJobObject(hJob, pi.hProcess);\r\n\r\n\tdo {\r\n\t\tQueryInformationJobObject(hJob, JobObjectBasicProcessIdList, pList, 10000, NULL);\r\n\r\n\t\tbJobAllEnd = TRUE;\r\n\r\n\t\tfor(i=0; i&lt;pList-&gt;NumberOfProcessIdsInList; i++)\r\n\t\t{\r\n\t\t\thProcess = OpenProcess(SYNCHRONIZE, FALSE, pList-&gt;ProcessIdList[i]);\r\n\t\t\tif(hProcess != NULL)\r\n\t\t\t{\r\n\t\t\t\tCloseHandle(hProcess);\r\n\t\t\t\tbJobAllEnd = FALSE;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tSleep(500);\r\n\t} while(!bJobAllEnd);\r\n\r\n\tGlobalFree(pList);\r\n\tCloseHandle(hJob);\r\n\r\n\tif (dwretval)\r\n\t\tGetExitCodeProcess(pi.hProcess, dwretval);\r\n\r\n\treturn TRUE;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to call external process and want to wait for it to terminate, you can call CreateProcess function and wait using WaitForSingeObject API. However, some process just launches child process and terminates immediately, resulting application still running but process terminates and returns to code. Below is sample code you can use in such case, where it monitors for process and child processes, and wait for all family process to terminate.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[331],"tags":[516,498,518,519,517,515,520,513,514],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/s3pJyQ-2284","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2284"}],"collection":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/comments?post=2284"}],"version-history":[{"count":2,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2284\/revisions"}],"predecessor-version":[{"id":2286,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2284\/revisions\/2286"}],"wp:attachment":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media?parent=2284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/categories?post=2284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/tags?post=2284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}