Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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++
Posted
Updated 21-Apr-16 3:33am
Comments
Mohibur Rashid 21-Apr-16 3:32am    
Run your operation in a thread. in the main thread(or may be other thread) look up the time of running. When the time is over, kill the child
Jochen Arndt 21-Apr-16 3:32am    
This can't be answered without knowing how your exe file is organised (what it is doing and how it is reacting to events like keyboard input).

If your program has some kind of loop waiting for events, just check the elpased time in that loop and exit when the time has expired.
enhzflep 21-Apr-16 3:32am    
Is the code you want to write going to be inside the program you'd like to close, or would you like PHP to open file A.exe, which will then be closed at a later time by file B.exe?
Member 11345334 21-Apr-16 4:05am    
"Is the code you want to write going to be inside the program you'd like to close?" yes.

try this code :

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

i hope it will help you
 
Share this answer
 
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.

C++
#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;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900