Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 23rd, 2004, 06:20 AM
Authorized User
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default terminate a thread problem


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;
     LONG l1,l2;

     CFile file;
     if( !file.Open(pView->m_LogFilePath, CFile::modeRead|CFile::shareDenyNone, &err ) )
          TCHAR szCause[255];
          err.GetErrorMessage(szCause, 255);
          return 0;
     l1 = (LONG)file.GetLength();
     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

     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.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Thread.abort nelly78 .NET Framework 2.0 10 April 4th, 2008 01:12 AM
Terminate Process (How To) Nova Beginning VB 6 3 October 14th, 2006 02:37 PM
connect to SQL Server terminate CarlosV SQL Server 2000 2 September 12th, 2005 09:37 PM
Problem in Thread Atif Khan C# 0 April 8th, 2005 06:57 AM
Form Hide and terminate raj_phoenix Excel VBA 4 January 17th, 2005 07:55 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.