如何在单击按钮时暂停读取文本文件并在 mfc vc++ 中的单击按钮时恢复它?
文件打开如下 CFileException fileException;
if (readFile.Open(strFilePath, CFile::modeRead, &fileException)
{
while (readFile.ReadString(strLine))
{
...
}
}
如何在单击按钮时暂停读取文本文件并在 mfc vc++ 中的单击按钮时恢复它?
文件打开如下 CFileException fileException;
if (readFile.Open(strFilePath, CFile::modeRead, &fileException)
{
while (readFile.ReadString(strLine))
{
...
}
}
将“阅读”移至单独的线程绝对是最好的方法。但是,您可以添加以下函数以允许在阅读时发生其他事件:
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...
}
这应该可行,但您可能会注意到应用程序在循环期间有点反应迟钝。希望这可以帮助。