0

如何在单击按钮时暂停读取文本文件并在 mfc vc++ 中的单击按钮时恢复它?

文件打开如下 CFileException fileException;

    if (readFile.Open(strFilePath, CFile::modeRead, &fileException)
{
while (readFile.ReadString(strLine))
{
...
}

}
4

2 回答 2

0

将“阅读”移至单独的线程绝对是最好的方法。但是,您可以添加以下函数以允许在阅读时发生其他事件:

void DoEvents()
{
  MSG msg;
  while( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  {
    ::TranslateMessage( &msg );
    ::DispatchMessage ( &msg );
  }
}

假设您的代码中有一个 volatile 标志来指示暂停/恢复,您可以执行以下操作:

while (readFile.ReadString(strLine)) 
{ 
  while( this->m_bPause == TRUE )
  {
    ::Sleep( 100 ); // Adjust as necessary
    DoEvents();
  }

  // continue reading...
} 

这应该可行,但您可能会注意到应用程序在循环期间有点反应迟钝。希望这可以帮助。

于 2012-03-16T08:24:11.683 回答
0

尝试将该代码移动到另一个线程,我认为如果此代码位于主线程内,但您必须从消息队列中查看消息,则任何解决方案都必须与重入作斗争。使用线程,您可以暂停它或使用一些同步原语来管理它的等待。

于 2012-03-14T08:31:03.137 回答