启动进程并稍后识别它
Starting Process and identify it later
现在我正在用 C++ 编写一个类似 cron 的调度程序。我的程序能够在特定时间开始作业并正确安排它们。
给我带来麻烦的是检查进程是否仍然存在。
所以我有一张地图JobMap;其中填充了作为键的进程句柄和作为值的作业。
我怎样才能把这两个值联系起来?我被困在每当我尝试 GetExitCodeProcess 它永远不会 returns STILL_ACTIVE 的地步。是否可以为流程设置一个唯一的 token/key 以便我可以识别它并将其与工作联系起来?
任何想法都会有很大的帮助,非常感谢!
void Cron::CheckRunningJobs()
{
DatNowObj Today;
DWORD exitCode = 0;
// now iterate through the executed jobs and check their state
for(MC_STL_PTR(MapPtr,JobMap)) //MapPtr is a pointer for my map
{
DatStrNowObj Today; //gives actual date
DWORD exitCode = 0;
PROT()<<"Job Handle:"<<MapPtr->first <<"Job in Map: "<<MapPtr->second.Job->getDescription()<<endl;
// store the exit code for later use
MapPtr->second.exitCode = GetExitCodeProcess(MapPtr->first, &exitCode);
if(GetExitCodeProcess(MapPtr->first, &exitCode) == STILL_ACTIVE)
{
PROT()<<"PROCESS STILL ACTIVE"<<endl;
}
else if(GetExitCodeProcess(MapPtr->first, &exitCode) == ERROR_INVALID_FUNCTION)
{
CloseHandle(MapPtr->first);
continue;
}
else if(MapPtr->second.execTime)
{
}
else if(GetExitCodeProcess(MapPtr->first, &exitCode) == 0)
{
CloseHandle(MapPtr->first);
JobMap.erase(MapPtr);
PROT()<<"Job in Map after Erase: "<<MapPtr->second.Job->getDescription() << "and Map size: " << JobMap.size()<<endl;
continue;
}
else
{
PROT()<<"Error:"<<GetLastError()<<endl;
continue;
}
}
}
BOOL WINAPI GetExitCodeProcess(
_In_ HANDLE hProcess,
_Out_ LPDWORD lpExitCode
);
它不会 return 直接退出代码。在 GetExitCodeProcess
了解更多信息
请阅读 the documentation user6545984 链接。你这里好像有点乱。
GetExitCodeProcess()
returns a BOOL
指示 function 是否成功,而不是过程。您正在尝试使用常量与 BOOL
进行比较,就好像它是一个 exitCode
。但它们是不兼容的类型。即使不是,我也不知道你为什么要一遍又一遍地调用这个函数!
获取退出码需要传入一个LPDWORD
指针来接收。调用函数 一次,将两个输出放入 right 变量 - 一个 return 值,一个输出参数 - 并响应这些适当地。
所以,你至少需要改变这个
MapPtr->second.exitCode = GetExitCodeProcess(MapPtr->first, &exitCode);
类似于
BOOL result = GetExitCodeProcess(MapPtr->first, &MapPtr->second.exitCode);
或者你想存储所述代码的任何其他地方 - 然后更新所有比较以匹配,如果需要,还可以将比较添加到 result
。
现在我正在用 C++ 编写一个类似 cron 的调度程序。我的程序能够在特定时间开始作业并正确安排它们。 给我带来麻烦的是检查进程是否仍然存在。
所以我有一张地图JobMap;其中填充了作为键的进程句柄和作为值的作业。 我怎样才能把这两个值联系起来?我被困在每当我尝试 GetExitCodeProcess 它永远不会 returns STILL_ACTIVE 的地步。是否可以为流程设置一个唯一的 token/key 以便我可以识别它并将其与工作联系起来?
任何想法都会有很大的帮助,非常感谢!
void Cron::CheckRunningJobs()
{
DatNowObj Today;
DWORD exitCode = 0;
// now iterate through the executed jobs and check their state
for(MC_STL_PTR(MapPtr,JobMap)) //MapPtr is a pointer for my map
{
DatStrNowObj Today; //gives actual date
DWORD exitCode = 0;
PROT()<<"Job Handle:"<<MapPtr->first <<"Job in Map: "<<MapPtr->second.Job->getDescription()<<endl;
// store the exit code for later use
MapPtr->second.exitCode = GetExitCodeProcess(MapPtr->first, &exitCode);
if(GetExitCodeProcess(MapPtr->first, &exitCode) == STILL_ACTIVE)
{
PROT()<<"PROCESS STILL ACTIVE"<<endl;
}
else if(GetExitCodeProcess(MapPtr->first, &exitCode) == ERROR_INVALID_FUNCTION)
{
CloseHandle(MapPtr->first);
continue;
}
else if(MapPtr->second.execTime)
{
}
else if(GetExitCodeProcess(MapPtr->first, &exitCode) == 0)
{
CloseHandle(MapPtr->first);
JobMap.erase(MapPtr);
PROT()<<"Job in Map after Erase: "<<MapPtr->second.Job->getDescription() << "and Map size: " << JobMap.size()<<endl;
continue;
}
else
{
PROT()<<"Error:"<<GetLastError()<<endl;
continue;
}
}
}
BOOL WINAPI GetExitCodeProcess(
_In_ HANDLE hProcess,
_Out_ LPDWORD lpExitCode
);
它不会 return 直接退出代码。在 GetExitCodeProcess
了解更多信息请阅读 the documentation user6545984 链接。你这里好像有点乱。
GetExitCodeProcess()
returns a BOOL
指示 function 是否成功,而不是过程。您正在尝试使用常量与 BOOL
进行比较,就好像它是一个 exitCode
。但它们是不兼容的类型。即使不是,我也不知道你为什么要一遍又一遍地调用这个函数!
获取退出码需要传入一个LPDWORD
指针来接收。调用函数 一次,将两个输出放入 right 变量 - 一个 return 值,一个输出参数 - 并响应这些适当地。
所以,你至少需要改变这个
MapPtr->second.exitCode = GetExitCodeProcess(MapPtr->first, &exitCode);
类似于
BOOL result = GetExitCodeProcess(MapPtr->first, &MapPtr->second.exitCode);
或者你想存储所述代码的任何其他地方 - 然后更新所有比较以匹配,如果需要,还可以将比较添加到 result
。