Click here to Skip to main content
15,891,828 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: WM_KILLFOCUS Pin
NC19-Aug-02 1:49
NC19-Aug-02 1:49 
QuestionIs there a way to see if a SOCKET is connected? Pin
Le centriste16-Aug-02 4:40
Le centriste16-Aug-02 4:40 
AnswerRe: Is there a way to see if a SOCKET is connected? Pin
Masaaki Onishi16-Aug-02 7:21
Masaaki Onishi16-Aug-02 7:21 
Questionhow to idenitify whether process is running or not Pin
tongc16-Aug-02 3:58
tongc16-Aug-02 3:58 
AnswerRe: how to idenitify whether process is running or not Pin
Tomasz Sowinski16-Aug-02 4:04
Tomasz Sowinski16-Aug-02 4:04 
GeneralRe: how to idenitify whether process is running or not Pin
tongc16-Aug-02 6:04
tongc16-Aug-02 6:04 
GeneralRe: how to idenitify whether process is running or not Pin
Tomasz Sowinski16-Aug-02 7:04
Tomasz Sowinski16-Aug-02 7:04 
GeneralRe: how to idenitify whether process is running or not Pin
Daniel Lohmann16-Aug-02 13:10
Daniel Lohmann16-Aug-02 13:10 
tcuong wrote:

while (WaitForMultipleObjects(5, proHandle, TRUE, 1000) == WAIT_TIMEOUT)
{
    for(i = 0; i < 5; i++)
    {
        if(proInfo[i].hProcess == NULL)
            runningProcess--;
    }
    printf(" %d ", runningProcess);
}


This code looks neither silly nor anybody wants to laugh on you, really!

First I thought the problem is the TRUE parameter for bWaitAll in your call to WaitForMultipleObjects(), (which is a common mistake) but then I realized that you want to loop until all handles reached signaled state (a process or thread handle becomes signaled if it terminates). Quite clever!

Your problem is, that a signaled handle is not set to NULL, it is still present. Therfore your process counter is never decremented. If you change the line
if(proInfo[i].hProcess == NULL)
    runningProcess--;

to
// check if handle is signaled (which means process has terminated)
if( WaitForSingleObject( proInfo[i].hProcess, 0 ) == WAIT_OBJECT_0 )
    runningProcess--;


it should work. (Note that I just wrote the above from scratch and did not test it Blush | :O )

However, your description intents that you want to check the status exactly every 1 second. The above checks it at least every 1 second. This is because the WaitForMultipleObjects() returns if either a handle becomes signaled or the timeout elapsed. Of course, this is a negligible detail in almost any case, but hey, you wrote you want to learn how to solve it with Win32 API Big Grin | :-D

One idea is to use Sleep() instead of WaitForMultipleObjects():

while ( runningProcess > 0)
{
    for(i = 0; i < 5; i++)
    {
        if( WaitForSingleObject( proInfo[i].hProcess, 0 ) == WAIT_OBJECT_0 )
            runningProcess--;
    }
    printf(" %d ", runningProcess);
    Sleep( 1000 );
}



BTW: If you bracket your code postings to the forum with <pre> and </pre> tags, the formatting will survive. This would makes it much more comfortable to read it.

--

Daniel Lohmann

http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there Big Grin | :-D )
GeneralRe: how to idenitify whether process is running or not Pin
tongc16-Aug-02 13:51
tongc16-Aug-02 13:51 
GeneralRe: how to idenitify whether process is running or not Pin
tongc16-Aug-02 14:57
tongc16-Aug-02 14:57 
GeneralRe: how to idenitify whether process is running or not Pin
tongc16-Aug-02 15:02
tongc16-Aug-02 15:02 
GeneralDefinition of Serialization Pin
Nick Parker16-Aug-02 3:56
protectorNick Parker16-Aug-02 3:56 
GeneralRe: Definition of Serialization Pin
Tomasz Sowinski16-Aug-02 4:08
Tomasz Sowinski16-Aug-02 4:08 
GeneralRe: Definition of Serialization Pin
Nick Parker16-Aug-02 4:20
protectorNick Parker16-Aug-02 4:20 
GeneralRe: Definition of Serialization Pin
Tomasz Sowinski16-Aug-02 4:29
Tomasz Sowinski16-Aug-02 4:29 
GeneralRe: Definition of Serialization Pin
Nick Parker16-Aug-02 4:38
protectorNick Parker16-Aug-02 4:38 
GeneralRe: Definition of Serialization Pin
Tomasz Sowinski16-Aug-02 4:44
Tomasz Sowinski16-Aug-02 4:44 
GeneralRe: Definition of Serialization Pin
Nick Parker16-Aug-02 4:47
protectorNick Parker16-Aug-02 4:47 
GeneralRe: Definition of Serialization Pin
Daniel Lohmann16-Aug-02 13:34
Daniel Lohmann16-Aug-02 13:34 
Generalado clone - some questions Pin
ns16-Aug-02 3:43
ns16-Aug-02 3:43 
GeneralKeeping the values in controls when closing and opening a Dialog Pin
Luis E. Cuadrado16-Aug-02 3:40
Luis E. Cuadrado16-Aug-02 3:40 
GeneralRe: Keeping the values in controls when closing and opening a Dialog Pin
mishgun16-Aug-02 3:43
mishgun16-Aug-02 3:43 
GeneralRe: Keeping the values in controls when closing and opening a Dialog Pin
Tomasz Sowinski16-Aug-02 3:46
Tomasz Sowinski16-Aug-02 3:46 
GeneralRe: Keeping the values in controls when closing and opening a Dialog Pin
Luis E. Cuadrado16-Aug-02 3:51
Luis E. Cuadrado16-Aug-02 3:51 
GeneralRe: Keeping the values in controls when closing and opening a Dialog Pin
Tomasz Sowinski16-Aug-02 3:52
Tomasz Sowinski16-Aug-02 3:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.