c++ - 启动使用 CreatEvent 停止的进程
问题描述
我正在尝试编写一个程序来获取进程信息。我的程序使用 createEvent 检查是否有任何进程停止?如果进程停止,程序将调用并重新启动它。对于 project1 和 project2 是任意两个项目。我希望当程序运行时,如果两个项目之一停止,程序会回调它们。
#include "stdafx.h"
#include<iostream>
#include <windows.h>
#include <time.h>
using namespace std;
HANDLE eventP1;
HANDLE eventP2;
HANDLE RunProcess(TCHAR* pszPath)
{
PROCESS_INFORMATION procInf;
STARTUPINFO startInf;
memset(&procInf, 0, sizeof(procInf));
memset(&startInf, 0, sizeof(startInf));
BOOL result;
result = CreateProcess(pszPath, NULL, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startInf, &procInf);
if (result == FALSE)
{
cout << "Process creation failed!" << endl;
return 0;
}
else
{
CloseHandle(procInf.hThread);
return procInf.hProcess;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hEndEvents[2];
DWORD dwRet;
eventP1 = CreateEvent(NULL, TRUE, FALSE, _T("Global\\eventP1"));
eventP2 = CreateEvent(NULL, TRUE, FALSE, _T("Global\\eventP2"));
hEndEvents[0] = OpenEvent(EVENT_ALL_ACCESS, FALSE, _T("Global\\eventP1"));
hEndEvents[1] = OpenEvent(EVENT_ALL_ACCESS, FALSE, _T("Global\\eventP2"));
if (hEndEvents[0] == NULL || hEndEvents[1] == NULL)
{
cout << "Open Event " << endl;
cout << GetLastError() << endl;
}
RunProcess(_T("D:\\Project_VuongDV\\VS2013\\DemoEventProcess\\Debug\\Project1.exe"));
RunProcess(_T("D:\\Project_VuongDV\\VS2013\\DemoEventProcess\\Debug\\Project2.exe"));
while (true)
{
dwRet = WaitForMultipleObjects(2, hEndEvents, FALSE, INFINITE);
if (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_OBJECT_0 + 1)
{
Sleep(100);
ResetEvent(hEndEvents[0]);
ResetEvent(hEndEvents[1]);
cout << "project1 and Project2 run" << endl;
SetEvent(hEndEvents[0]);
SetEvent(hEndEvents[1]);
}
else
{
cout << "WAIL FALSE" << endl;
cout << "Thread error is" << GetLastError() << endl;
RunProcess(_T("D:\\Project_VuongDV\\VS2013\\DemoEventProcess\\Debug\\Project1.exe"));
RunProcess(_T("D:\\Project_VuongDV\\VS2013\\DemoEventProcess\\Debug\\Project2.exe"));
}
}
CloseHandle(hEndEvents[1]);
CloseHandle(hEndEvents[2]);
system("pause");
return 0;
}
解决方案
推荐阅读
- docker - 启用 SELinux 的服务器上的 Docker 挂载卷
- excel - 需要 VBA 代码方面的帮助 转换 CSV > XLS
- node.js - 在运行时使用 pkg ERR_INSPECTOR_NOT_AVAILABLE 打包节点
- python - Matplotlib,防止情节显示
- php - 如何从 PHP 和 PHP 文件中排除 HTML 仍然能够与 HTML 文件链接?
- r - 如何找到列中前一个值之间的较高值
- javascript - html、js 和 css 中的 Js 逻辑基本用户名和密码脚本
- javascript - 表单序列化不接受 jQuery 中的图像
- sql - 为什么 Oracle 没有使用正确的索引?
- javascript - 将 MediaFile 转换为 Blob