我需要每10秒检查1.exe,2.exe是否正在运行,如果没有运行则运行它。 [英] I need 1.exe, 2.exe to be checked every 10 seconds whether it is running or not and if not running then run it.

查看:69
本文介绍了我需要每10秒检查1.exe,2.exe是否正在运行,如果没有运行则运行它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_WIN32_WINNT 0x0500 // define needed to use GetConsoleWindow() function
#include <windows.h>
#include <tlhelp32.h>
#include <string.h>
 
int main()
{    
    BOOL is_running = 0;
    DWORD pid = 0;
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
 
    //start of the checking loop, check happens once in every 10 seconds
    while(1){ 
        //take a snapshot of windows running processes
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
        Process32First(hProcessSnapshot, &pe32);
 
        //compare the name of each process in the process list with "sleep.exe"
        while (Process32Next(hProcessSnapshot, &pe32)) {
            if (!strcmp("1.exe", pe32.szExeFile)) {
                is_running = 1; //change is_running status if name matches
                break;
            }
        }
 
        if(!is_running) system("start 1.exe"); //start the process if it's not running
        is_running = 0; //restore is_running status
        Sleep(10000); //sleep for 10 seconds
       
        while (Process32Next(hProcessSnapshot, &pe32)) {
            if (!strcmp("2.exe", pe32.szExeFile)) {
                is_running = 1; //change is_running status if name matches
                break;
            }
        }
 
        if(!is_running) system("start 2.exe"); //start the process if it's not running
        is_running = 0; //restore is_running status
        Sleep(10000); //sleep for 10 seconds        
    }
 
    return 0;
}





我的尝试:



想要简化我的问题



What I have tried:

want to simplify my question

推荐答案

while(1){
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
// ...
        HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);



您正在尝试重新定义相同的变量。从第二次出现中删除类型( HANDLE )。或者如果你想要两个这样的句柄,那么重命名其中一个。


You are trying to redefine the same variable. Remove the type (HANDLE) from the second occurrence. Or if you want two such handles then rename one of them.


这篇关于我需要每10秒检查1.exe,2.exe是否正在运行,如果没有运行则运行它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆