greless
Greless
采纳率45%
2017-02-28 12:26 阅读 1.5k
已采纳

MFC CreateProcess问题

10

我在CreateProcess运行1.bat 程序完成后,静态框显示1.bat finish,然后又CreateProcess运行2.bat 程序完成后,静态框显示2.bat finish.实际情况是运行完1.bat静态框不显示,要2.bat运行完他们才一起显示出来
void CMFCApplication2Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
//HANDLE m_hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, this, 0, NULL);
//SetTimer(1, 10, NULL);
CString strText;
m_Static.GetWindowText(strText);
strText += _T("\tstart!\n");
m_Static.SetWindowText(strText);
LPTSTR szCmdline = _tcsdup(TEXT("1.bat"));
//xx(szCmdline);
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));    
// Start the child process. 
if (!CreateProcess(NULL,   // No module name (use command line)
    szCmdline,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)           // Pointer to PROCESS_INFORMATION structure
    )
{
    printf("CreateProcess failed (%d).\n", GetLastError());
    /*return 0;*/
    exit(0);
}

// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);             
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

m_Static.GetWindowText(strText);
strText += _T("\t1.finish !\n");
m_Static.SetWindowText(strText);
LPTSTR szCmdline1 = _tcsdup(TEXT("2.bat"));
STARTUPINFO si1;
PROCESS_INFORMATION pi1;

ZeroMemory(&si1, sizeof(si1));
si1.cb = sizeof(si1);
ZeroMemory(&pi1, sizeof(pi1));
// Start the child process. 
if (!CreateProcess(NULL,   // No module name (use command line)
    szCmdline1,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)           // Pointer to PROCESS_INFORMATION structure
    )
{
    printf("CreateProcess failed (%d).\n", GetLastError());
    /*return 0;*/
    exit(0);
}
// Wait until child process exits.
WaitForSingleObject(pi1.hProcess, INFINITE);
CloseHandle(pi1.hProcess);
CloseHandle(pi1.hThread);
//xx(szCmdline1);
m_Static.GetWindowText(strText);
strText += _T("\t2.finish  !\n");
m_Static.SetWindowText(strText);

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    oyljerry oyljerry 2017-02-28 12:33

    你这里waitforsingleobject阻塞UI线程了。你需要把这里所有代码都放到线程中。然后线程发消息给主线程来更新控件

    点赞 评论 复制链接分享
  • greless Greless 2017-02-28 12:29

    图片说明

    点赞 评论 复制链接分享

相关推荐