/ src / common / utils / ProcessWaiter.h
ProcessWaiter.h
 1  #include <functional>
 2  #include <string>
 3  #include <Windows.h>
 4  #include <thread>
 5  
 6  namespace ProcessWaiter
 7  {
 8      void OnProcessTerminate(std::wstring parent_pid, std::function<void(DWORD)> callback)
 9      {
10          DWORD pid = 0;
11          try
12          {
13              pid = std::stol(parent_pid);
14          }
15          catch (...)
16          {
17              if (callback)
18              {
19                  callback(ERROR_INVALID_PARAMETER);
20              }
21              return;
22          }
23          std::thread([=]() {
24              HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid);
25              if (process != nullptr)
26              {
27                  if (WaitForSingleObject(process, INFINITE) == WAIT_OBJECT_0)
28                  {
29                      CloseHandle(process);
30                      if (callback)
31                      {
32                          callback(ERROR_SUCCESS);
33                      }
34                  }
35                  else
36                  {
37                      CloseHandle(process);
38                      if (callback)
39                      {
40                          callback(GetLastError());
41                      }
42                  }
43              }
44              else
45              {
46                  if (callback)
47                  {
48                      callback(GetLastError());
49                  }
50              }
51          }).detach();
52      }
53  }