Terminate process (WaitableTimer) 
Author Message
 Terminate process (WaitableTimer)

I created a single process using CreateProcess() and want to close this
process automatically using WaitableTimer, and the code looks like:

int main()
{
        ....
        _int64 endTime;
        LARGE_INTEGER quitTime;
        HANDLE wTimer;
        const int k=5; // in seconds

        strcpy(lpCommandLine, "C:\\WINNT\\SYSTEM32\\CALC.EXE");
        if (!CreateProcess(NULL, lpCommandLine,
                           NULL, &threadSA,
                           FALSE,
                           HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
                           NULL, NULL,
                           &startInfo,
                           &processInfo))
        {
                fprintf(stderr, "Error %d\n", GetLastError());
                ExitProcess(1);
        }
        wTimer = CreateWaitableTimer(NULL, FALSE, NULL);
        endTime = k * 10000000;     // convert to 100 ns
        quitTime.LowPart = (DWORD)(endTime & 0xFFFFFFFF);
        quitTime.HighPart = (LONG)(endTime >> 32);
        if (SetWaitableTimer(wTimer, &quitTime, 0, NULL, NULL, FALSE))
                WaitForSingleObject(wTimer, INFINITE);
        ExitProcess(1);
        return 0;

Quote:
}

The calculator windows was created.
I need to close automatically the window after 5 second.
But, it doesn't work. The window still open.

Is there any mistake with my code ?
Any helps will be highly appreciated. Thanks.

Wiria

PS: I'm so sorry if the message is too long....



Wed, 08 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Quote:

> I created a single process using CreateProcess() and want to close this
> process automatically using WaitableTimer, and the code looks like:

> int main()
> {
>         ....
>         _int64 endTime;
>         LARGE_INTEGER quitTime;
>         HANDLE wTimer;
>         const int k=5; // in seconds

>         strcpy(lpCommandLine, "C:\\WINNT\\SYSTEM32\\CALC.EXE");
>         if (!CreateProcess(NULL, lpCommandLine,
>                            NULL, &threadSA,
>                            FALSE,
>                            HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
>                            NULL, NULL,
>                            &startInfo,
>                            &processInfo))
>         {
>                 fprintf(stderr, "Error %d\n", GetLastError());
>                 ExitProcess(1);
>         }
>         wTimer = CreateWaitableTimer(NULL, FALSE, NULL);
>         endTime = k * 10000000;     // convert to 100 ns
>         quitTime.LowPart = (DWORD)(endTime & 0xFFFFFFFF);
>         quitTime.HighPart = (LONG)(endTime >> 32);
>         if (SetWaitableTimer(wTimer, &quitTime, 0, NULL, NULL, FALSE))
>                 WaitForSingleObject(wTimer, INFINITE);
>         ExitProcess(1);
>         return 0;
> }

> The calculator windows was created.
> I need to close automatically the window after 5 second.
> But, it doesn't work. The window still open.

> Is there any mistake with my code ?
> Any helps will be highly appreciated. Thanks.

> Wiria

> PS: I'm so sorry if the message is too long....

There is no code here to close the Calculator!  ExitProcess closes your process
but does not affect other processes you created.  (I guess you are a unix type?)
This will close the Calculator:

CWnd* pCalc = CWnd::FindWindow("SciCalc", NULL);
if (pCalc)
        pCalc->PostMessage(WM_CLOSE, 0, 0);

Not all programs will close this way, but most will. It is the same as clicking
on the program's X close button.  I used the Spy++ tool to find that Calc's main
window class name is "SciCalc".  If you are not using MFC the API versions of
FindWindow and PostMessage will do the same thing.

--
Scott McPhillips [VC++ MVP]



Wed, 08 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)


Fri, 19 Jun 1992 00:00:00 GMT  
 Terminate process (WaitableTimer)
Hi Scott,

Thanks a lot for your explanation!

Now, I can close my Calculator window,...
But, the problem is, my SetWaitableTimer() doesn't work,.. I mean, after
I did PostMessage(WM_CLOSE,0,0), the calculator window is automatically
closed without waiting for the timer.

My programs now looks like:
...
int main()
{
   ...
   const int k=2; // in seconds

   strcpy(lpCommandLine, "E:\\WINNT\\SYSTEM32\\CALC.EXE");

        if (!CreateProcess(NULL, lpCommandLine,
                               NULL, &threadSA,
                                           FALSE,

HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
                                           NULL, NULL,
                                           &startInfo,
                                           &processInfo))
        {
                fprintf(stderr, "CreateProcess failed on error %d\n",
GetLastError());
                ExitProcess(1);
        }

   wTimer = CreateWaitableTimer(NULL, FALSE, NULL);

   endTime = k * 10000000;     // convert to 100 ns
   quitTime.LowPart = (DWORD)(endTime & 0xFFFFFFFF);
   quitTime.HighPart = (LONG)(endTime >> 32);

   if (SetWaitableTimer(wTimer, &quitTime, 0, NULL, NULL, FALSE))
        WaitForSingleObject(wTimer, INFINITE);

   HWND pProcess = FindWindow("SciCalc",0);
   PostMessage(pProcess, WM_CLOSE,0,0);

   ExitProcess(1);
   return 0;

Quote:
}

After WaitForSingleObject(), the WM_CLOSE automatically close my
calculator window without waiting any timer.

Is there any mistake again ?

Once again, any suggestion will be highly appreciated.

Thank you,
Wiria Chandra

Quote:
-----Original Message-----

There is no code here to close the Calculator!  ExitProcess closes your
process
but does not affect other processes you created.  (I guess you are a
unix type?)
This will close the Calculator:

CWnd* pCalc = CWnd::FindWindow("SciCalc", NULL);
if (pCalc)
        pCalc->PostMessage(WM_CLOSE, 0, 0);

Not all programs will close this way, but most will. It is the same as
clicking
on the program's X close button.  I used the Spy++ tool to find that
Calc's main
window class name is "SciCalc".  If you are not using MFC the API
versions of
FindWindow and PostMessage will do the same thing.

--
Scott McPhillips [VC++ MVP]



Thu, 09 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Quote:

> But, the problem is, my SetWaitableTimer() doesn't work,.. I mean, after
> I did PostMessage(WM_CLOSE,0,0), the calculator window is automatically
> closed without waiting for the timer.

When you call

WaitForSingleObject(wTimer, INFINITE);

it's the calling thread that waits.
In your program it's your program's only thread.
After setting the timer it waits for 2 seconds then
posts the quit message. After the message is posted
there is no timeout. Windows delivers the message
to the target application and the latter processes the
message in it's message loop.

Sergei

Quote:
> My programs now looks like:
> ...
> int main()
> {
>    ...
>    const int k=2; // in seconds

>    strcpy(lpCommandLine, "E:\\WINNT\\SYSTEM32\\CALC.EXE");

> if (!CreateProcess(NULL, lpCommandLine,
>                NULL, &threadSA,
>    FALSE,

> HIGH_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
>    NULL, NULL,
>    &startInfo,
>    &processInfo))
> {
> fprintf(stderr, "CreateProcess failed on error %d\n",
> GetLastError());
> ExitProcess(1);
> }

>    wTimer = CreateWaitableTimer(NULL, FALSE, NULL);

>    endTime = k * 10000000;     // convert to 100 ns
>    quitTime.LowPart = (DWORD)(endTime & 0xFFFFFFFF);
>    quitTime.HighPart = (LONG)(endTime >> 32);

>    if (SetWaitableTimer(wTimer, &quitTime, 0, NULL, NULL, FALSE))
> WaitForSingleObject(wTimer, INFINITE);

>    HWND pProcess = FindWindow("SciCalc",0);
>    PostMessage(pProcess, WM_CLOSE,0,0);

>    ExitProcess(1);
>    return 0;
> }

> After WaitForSingleObject(), the WM_CLOSE automatically close my
> calculator window without waiting any timer.

> Is there any mistake again ?

> Once again, any suggestion will be highly appreciated.

> Thank you,
> Wiria Chandra

> -----Original Message-----
> There is no code here to close the Calculator!  ExitProcess closes your
> process
> but does not affect other processes you created.  (I guess you are a
> unix type?)
> This will close the Calculator:

> CWnd* pCalc = CWnd::FindWindow("SciCalc", NULL);
> if (pCalc)
> pCalc->PostMessage(WM_CLOSE, 0, 0);

> Not all programs will close this way, but most will. It is the same as
> clicking
> on the program's X close button.  I used the Spy++ tool to find that
> Calc's main
> window class name is "SciCalc".  If you are not using MFC the API
> versions of
> FindWindow and PostMessage will do the same thing.

> --
> Scott McPhillips [VC++ MVP]



Thu, 09 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)
Thanks Sergei!

So, what should I do so that the PostMessage() can wait after
WaitableTimer is set ?

-I'm sorry, I'm new in Visual C++..:)-

Wiria

Quote:
-----Original Message-----

When you call

WaitForSingleObject(wTimer, INFINITE);

it's the calling thread that waits.
In your program it's your program's only thread.
After setting the timer it waits for 2 seconds then
posts the quit message. After the message is posted
there is no timeout. Windows delivers the message
to the target application and the latter processes the
message in it's message loop.

Sergei



Thu, 09 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Quote:

> Thanks Sergei!

> So, what should I do so that the PostMessage() can wait after
> WaitableTimer is set ?

After you call PostMessage you do not control anything.
You can control the time to call PostMessage and this
is already done in your program.
It's difficult to understand what you're trying to achive.
If you want your Calculator to be closed at some time
after you leave your main program, then probably you
need to write a small standalone closing program and
schedule it using, say, AT command.

Sergei



Fri, 10 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)
Thanks again Sergei!

Yes, that's what I'd like to do:
My calculator window will be closed after 2 seconds.

But, with my understanding, you said that PostMessage will be process
after the time that I defined with WaitableTimer.
If I set 2 seconds (after WaitForSingleObject()), and then my program
will process PostMessage(WM_CLOSE), why when I run the program, it will
close the window immediately, without waiting 2 seconds ?
So, I mean in my program now, after WaitForSingleObject(), it should
wait until 2 seconds before run PostMessage(), is it correct ?

I'm sorry if my question seems to be too basic, I am new in this process
creation...:)

Once again, thank you for your explanation, I really appreciate it!

Wiria

Quote:
-----Original Message-----
> So, what should I do so that the PostMessage() can wait after
> WaitableTimer is set ?

After you call PostMessage you do not control anything.
You can control the time to call PostMessage and this
is already done in your program.
It's difficult to understand what you're trying to achive.
If you want your Calculator to be closed at some time
after you leave your main program, then probably you
need to write a small standalone closing program and
schedule it using, say, AT command.

Sergei



Fri, 10 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Quote:

> Thanks again Sergei!

> Yes, that's what I'd like to do:
> My calculator window will be closed after 2 seconds.

Is there some reason you don't simply call Sleep(2000) ?

--
Scott McPhillips [VC++ MVP]



Fri, 10 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Thanks!
It works correctly!

Wiria

Quote:
-----Original Message-----
I didn't read your code carefully. In order to specify relative times
you should use nagative values:

quitTime.QuadPart = - 10000000 * static_cast<LONGLONG>(k);

Sergei



Fri, 10 Jan 2003 03:00:00 GMT  
 Terminate process (WaitableTimer)

Quote:

> Thanks again Sergei!

> So, I mean in my program now, after WaitForSingleObject(), it should
> wait until 2 seconds before run PostMessage(), is it correct ?

Please pardon me.
I didn't read your code carefully. In order to specify relative times
you should use nagative values:

Quote:
> > So, what should I do so that the PostMessage() can wait after
> > WaitableTimer is set ?

quitTime.QuadPart = - 10000000 * static_cast<LONGLONG>(k);

Sergei



Sat, 11 Jan 2003 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Terminate the process using WaitableTimer

2. Terminating a child process

3. Process suddenly terminates without notification

4. terminate process

5. Debugged Process will not Terminate under Win2K

6. Process suddenly terminates without notification

7. Terminate Process and SecurityIno ?

8. Find and terminate a process under WIN98

9. How to terminate a process

10. terminating an out of process application

11. terminate or Exit process, need help!

12. Terminate or Exit Process, need help!

 

 
Powered by phpBB® Forum Software