用CreateProcess创建了一个子进程,不显示命令行窗口,想中途终止,发现用TerminateProcess然后WaitForSingleObject,子进程能够正确退出,但是得到的结果不对。
命令行窗口中单独运行子进程(参数相同),可以用q退出,可以ctrl+c退出。
TerminateProcess和ctrl+c的效果不一样的啊,子进程没有消息循环,命令行程序。
请教应该如何正确的结束子进程。

解决方案 »

  1.   

    你是想实现多线程吗??,用CreateProcess 好像实现不了多线程的。
    在MSDN上面查了一下CreateProcess,
    The CreateProcess function is used to run a new program. In addition to creating a process, CreateProcess also creates a thread object. The thread is created with an initial stack whose size is described in the image header of the specified program’s executable file. The thread begins execution at the image’s entry point.
      

  2.   

    to yyhappy:
    不是多线程,是多进程,开启一个新的程序。to chenqisuo:
    CreateProcess和TerminateProcess的子进程句柄是一样的。
      

  3.   

    当然不以用TerminateProcess了,应该用WaitForSingleObject以等进程正常结束。
      

  4.   

    发送WM_CLOSE等让子进程自己退出
      

  5.   

    WaitForsingleObject(子进程句柄,INFINITE);
    等待正常结束。
    如果要强制结束,SendMessage(WM_CLOSE..........TerminateProcess函数是个异步运行的函数,也就是说当函数返回时,目标进程不一定已经结束了,它可能会在将来的某个时候结束。