Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 23rd, 2004, 06:20 AM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.