如果你的进程创建成功,还要在WaitForSingleObject(stProcessInfo.hProcess, INFINITE)之前调用ClsoeHandle(stProcessInfo.hThread)释放自进程的主线程对象

解决方案 »

  1.   

    去掉waitforsingleobject()不就可以用定时器了吗?
      

  2.   

    To Viper:
    我还需要判断进程是否结束,何时结束,所以去掉之后就搞不定了,请指教。
      

  3.   

    你是用WM_TIMER吧,它是向你的父进程的消息队列发消息,但是你用WaitForSingleObject,子进程不结束,你的父进程就会一直阻塞,无法存取消息队列。
    解决方法有二:
    1。你可以试试timeSetEvent,它用的回调函数是运行在一个独立线程的,缺点是很多API在这里都不能用,只有象PostMessage之类的能用。
    2。WaitForSingleObject(stProcessInfo.hProcess, 100);比如100ms就停止一下阻塞,存取消息队列,然后再调一下WaitForSingleObject(stProcessInfo.hProcess, 100);
      

  4.   

    WaitForSingleObject(stProcessInfo.hProcess, INFINITE);这一句挂起了主进程。
    如果你需要随时检查子进程是否结束,可以定时(Timer)/主线程Idle时用:
    if(WaitForSingleObject(stProcessInfo.hProcess, 0) == WAIT_OBJECT_0) {...}
    检察,或在子线程中WaitForSingleObject(stProcessInfo.hProcess, INFINITE);
      

  5.   

    以上两位老兄的方法都可行,但不是最好的。建议你在自己的消息循环中用MsgWaitForMultipleObjects()函数。// message loop
    int MessageLoop 

    HANDLE* lphObjects,  // handles that need to be waited on 
         int     cObjects     // number of handles to wait on 
       )

        while( true )
        {
            // block-local variable 
            DWORD result ; 
            MSG msg ;  // peek new message 
            while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) 
            { 
             // if have new messages do following process
            
                // If it's a quit message, we're out of here.
                if( WM_QUIT == msg.message )  
                    return 1; 
                // Otherwise, dispatch the message.
                DispatchMessage(&msg); 
            }  // Wait for any message sent or posted to this queue 
            // or for one of the passed handles be set to signaled.
            result = MsgWaitForMultipleObjects( cObjects, lphObjects, FALSE, INFINITE, QS_ALLINPUT|QS_TIMER );         // The result tells us the type of event we have.
            if( (WAIT_OBJECT_0 + cObjects) == result )
            {
                // New messages have arrived. 
                // Continue to the top of the always while loop to 
                // dispatch them and resume waiting.
                continue;
            } 
            else 
            { 
             // do yoursomething
                // One of the handles became signaled. 
                DoStuff( result - WAIT_OBJECT_0 ) ; 
            } 
        } 
    } 你在createprocess之后。
    调用MessageLoop( &hProcess, 1 );
    即可。visit MSDN foe detail
      

  6.   


    那个DoStuff(..)函数表示你在事件发生后(在你的情况下就是创建的进程结束了)所做的事,你需要定义这个函数。