MFC:当进程在win32文本区域(mfc应用程序)中执行时,异步(同时)显示进程的输出。 [英] MFC: Display output of a process asynchronously(concurrently) while process is in execution in a win32 text area (mfc application)

查看:137
本文介绍了MFC:当进程在win32文本区域(mfc应用程序)中执行时,异步(同时)显示进程的输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MFC中开发一个应用程序,它可以启动一个冗长的控制台进程,并在32位Windows应用程序的文本区域上同时提供输出。

I want to develop an application in MFC which could start a lengthy console process and give its output concurrently on a text area of 32bit windows application.

,但它只在进程终止后才显示输出。
我试过_popen,它适用于基于控制台的应用程序,但不兼容win32应用程序。

I used pipes, but it display the output only after the process has terminated. I tried _popen, it works for console based application, but not compatible with win32 application.

在搜索互联网时,我发现大量代码使用CLR,

while searching internet, I found numerous code using CLR, but I need some way in MFC, without use of .Net.. Plz help.

THANKS in Advance: - )

THANKS in Advance :-)

这是我的代码启动应用程序:

Here's my code that start the application:

void CAppMgr_BackupsDlg::ExecuteExternalFile(CString csExeName, CString csArguments)
{

CString csExecute;
csExecute=csExeName + " " + csArguments;

SECURITY_ATTRIBUTES secattr; 
ZeroMemory(&secattr,sizeof(secattr));
secattr.nLength = sizeof(secattr);
secattr.bInheritHandle = TRUE;

HANDLE rPipe, wPipe;

//Create pipes to write and read data
CreatePipe(&rPipe,&wPipe,&secattr,0);

STARTUPINFO sInfo; 
ZeroMemory(&sInfo,sizeof(sInfo));
PROCESS_INFORMATION pInfo; 
ZeroMemory(&pInfo,sizeof(pInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESTDHANDLES;
sInfo.hStdInput=NULL; 
sInfo.hStdOutput=wPipe; 
sInfo.hStdError=wPipe;
char command[1024]; 
strcpy(command, csExecute.GetBuffer(csExecute.GetLength()));

//Create the process here.
CreateProcess(0, command,0,0,TRUE,
      NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CloseHandle(wPipe);

//now read the output pipe here.
char buf[100];
DWORD reDword; 
CString m_csOutput,csTemp;
BOOL res;
do
{
    res=::ReadFile(rPipe,buf,100,&reDword,0);
    csTemp=buf;
    m_csOutput=csTemp.Left(reDword);
            DisplayToTextArea(m_csOutput);
}
while(res);
}



PS:我在x86 windows 7上使用Visual studio 2010。这个代码要与winPE集成,因此强烈需要MFC。

PS: I am using Visual studio 2010 on x86 windows 7. I am making this code to be integrated with winPE, therefore strongly need MFC.

推荐答案

问题似乎是读取管道的循环阻塞您的应用程序的UI主线程,以便您的对话框不会更新,直到循环完成。

The problem seems the loop to read the pipe is blocking the UI main thread of your application so that your dialog is not updated until the loop finished.

有一些事情你可以做,以解决这个问题,但最简单的方法是在调用DisplayToTextArea之后,向您的do-while循环添加一个消息处理循环:

There are some things you can do to solve this problem but the easiest way would be to add a message processing loop to your do-while loop after calling DisplayToTextArea:

 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0)) 
 {
    TranslateMessage(&msg);
    DispatchMessage(&msg);  // send to window proc
 } 

这篇关于MFC:当进程在win32文本区域(mfc应用程序)中执行时,异步(同时)显示进程的输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆