如何在C ++中关闭.exe文件? [英] How to close a .exe file in C++ ?

查看:354
本文介绍了如何在C ++中关闭.exe文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在外部运行C ++的 .exe 文件,例如从PhP运行。现在我想在一段时间后通过在C ++中编写代码来退出文件。我怎么能这样做?



我的尝试:



我试图通过在C ++中编写代码来退出它

I am running an .exe file of C++ externally such as from PhP. Now i want to exit the file after a certain time by writing code inside C++. How can i do that?

What I have tried:

I am trying to exit it by writing code inside C++

推荐答案

试试这段代码:



system( taskkill / F / T / IM file.exe);



i希望它会帮到你
try this code :

system("taskkill /F /T /IM file.exe");

i hope it will help you


好的,然后在在这种情况下,你可以启动一个计时器,设置你希望程序运行的时间段。当您收到WM_TIMER消息时,您知道此时间段已经过去并且是时候退出了。

您可以使用以下控制台模式程序来实现此目的。在我的系统上,它一直运行5.02到5.07秒。



我写的方式,在这里做的事情部分的代码不会当时间过去时被中断,你必须不断执行要完成的任务的一小部分,以便检查WM_TIMER消息是否已经进入。即你可以计算一个图像的单个像素的颜色,或1/10秒的音频或其他。



不知道你在尝试什么实际做了N秒,我不知道是否值得启动另一个线程来实际做东西,而主线程除了等待定时器消息之外什么都不做。



Okay, then in that case you can start a timer, set with the duration of the period you'd like the program to run for. When you get the WM_TIMER message, you know this period has elapsed and it's time to exit.
You can use the following console-mode program to achieve this. On my system, it consistently runs for between 5.02 and 5.07 seconds.

The way I've written it, code in the "do stuff here" section won't be interrupted when the time elapses, you must continually perform small parts of the task to be completed in order to check to see if a WM_TIMER message has come in yet. I.e you might calculate the colour of a single pixel of an image, or a 1/10 of a second of audio or whatever.

Without knowing what you're trying to actually do for N number of seconds, I can't know if it's worth spinning up another thread to actually do stuff while the main thread does nothing other than waiting for timer messages.

#include <cstdio>
#include <windows.h>

int main()
{
    MSG msg;
    int shouldContinue = true;
    bool msgFound = false;

    DWORD curThreadId = GetCurrentThreadId();

    while (PostThreadMessage(curThreadId, WM_USER, 0, 0) == 0)
    {
        Sleep(10);
    }

    // set the program to run for 5 seconda
    int nMilliSeconds = 5 * 1000;
    SetTimer(NULL, 0, nMilliSeconds, NULL);

    while (shouldContinue == true)
    {
        // see if there are any messages in the queue
        msgFound = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
        if (msgFound == true)
        {
            // if there are, check if it's signalling the end of the timing period
            if (msg.message == WM_TIMER)
                shouldContinue = false;
        }

        else
        {
            // do stuff here
            printf(".");
        }
    }
    return 0;
}


这篇关于如何在C ++中关闭.exe文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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