terminate a thread problem
Hi
I started a thread to check for a file change using the below code
Now on I have abutton on my UI. Onclick of the button I want to stop the above thread.
CWinThread* m_UpdateMonitorThread = AfxBeginThread(CheckFileUpdates,(LPVOID)this)
UINT CheckFileUpdates(LPVOID lp)
{
//Get the document pointer
CLogViewerAppView* pView = (CLogViewerAppView*)lp;
CLogViewerAppDoc* pDoc = pView->GetDocument();
CFileException err;
CFileStatus st;
CFile::GetStatus(pView->m_LogFilePath,st);
LONG l1,l2;
CFile file;
if( !file.Open(pView->m_LogFilePath, CFile::modeRead|CFile::shareDenyNone, &err ) )
{
TCHAR szCause[255];
err.GetErrorMessage(szCause, 255);
AfxMessageBox(szCause);
return 0;
}
l1 = (LONG)file.GetLength();
file.Close();
CFile file1;
LONG size_1 = (LONG)st.m_size;
int len = pView->m_LogFilePath.GetLength()-pView->m_LogFileName.GetLength()-1;
CString dir = pView->m_LogFilePath.Left(len);
HANDLE hDir = CreateFile( dir, // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS, // file attributes
NULL // file with attributes to copy
);
FILE_NOTIFY_INFORMATION Buffer[1024];
DWORD BytesReturned;
while( ReadDirectoryChangesW(
hDir, // handle to directory
&Buffer, // read results buffer
sizeof(Buffer), // length of buffer
TRUE, // monitoring option
FILE_NOTIFY_CHANGE_SIZE, // filter conditions
&BytesReturned, // bytes returned
NULL, // overlapped buffer
NULL// completion routine
))
{
if ( WaitForSingleObject(pView->m_StopEvent, INFINITE) == WAIT_OBJECT_0 )
{
OutputDebugString(_T("\n KIIGSRETG \n"));
return 0; //end the thraed
}
CString fileName(Buffer[0].FileName);
fileName = fileName.Left(Buffer[0].FileNameLength / 2);
if(fileName.CompareNoCase(pView->m_LogFileName) == 0)
{
//Do the work
}
}
return 0;
}
But the m_StopEvent event is not caught beacuse the control is stuck at while( ReadDirectoryChangesW) I mean it not going inside the while loop unless ReadDirectoryChangesW returns true. Hence WaitForSingleObject is not checked at all..
Please help me to proceed further.
Thanks
Madhavi
|