Threading & CStringArray - long post warning
-
Hey, Its a pretty simple idea really, I'm trying to make a file searcher thats uses a worker thread to do the searching so the user can hit cancel at any time and it won't hang the program. So far the thread is working, it will search and add a list of all the files I want into a CStringArray, but the problem is that I need to use this CStringArray in the main program so it can store the list.
A brief code summary is ---To begin the Thread--- CWinThread* pThread = AfxBeginThread(ListFunc,this); HANDLE hThread = pThread->m_hThread; WaitForSingleObject(hThread,INFINITE); ---To get the search running--- UINT ListFunc(LPVOID pParam) { CMyDlg* local = (CMyDlg*)pParam; local->BuildFileList(local->m_startDir); return 0; } ---To do the actual searching--- void CMyDlg::BuildFileList(CString startingDir) { chdir(startingDir); // start working for files CFileFind finder; BOOL bWorking = finder.FindFile(); int counter = 0; CStringArray pathArray; pathArray.SetSize(500,500); while (bWorking && running) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; if (finder.IsDirectory()) { CString str = finder.GetFilePath(); TRACE("str = %s\n",str); BuildFileList(str); } CString temp = finder.GetFilePath(); if(temp.Find(".wav",0)!= -1) { TRACE("Found wav file = %s\n",temp); temp.Delete(0,(m_startDir.GetLength()+1)); TRACE("wav file now is = %s\n",temp); pathArray[counter] = temp; } filePathArray[counter] = pathArray[counter]; counter++; } noOfFiles = counter+1; filePathArray.Copy(pathArray); <---- Heres the problem as I see it finder.Close(); }
The problem as far as I know is the filePathArray.Copy function near the end. The filePathArray is a CStringArray defined in the actual Dlg class that I want to store all of the details. But as it is, when the thread ends after a WaitForSingleObject(hThread,INFINITE); The filePathArray is empty :( Does anyone know where I'm going wrong with this thing before I go slightly loopy? At one point I had it copying a list of stuff I didn't want (a list of files in a dir, not .wav files) but I cannot get it to copy anything at all now. Thanks for looking lads :) -
Hey, Its a pretty simple idea really, I'm trying to make a file searcher thats uses a worker thread to do the searching so the user can hit cancel at any time and it won't hang the program. So far the thread is working, it will search and add a list of all the files I want into a CStringArray, but the problem is that I need to use this CStringArray in the main program so it can store the list.
A brief code summary is ---To begin the Thread--- CWinThread* pThread = AfxBeginThread(ListFunc,this); HANDLE hThread = pThread->m_hThread; WaitForSingleObject(hThread,INFINITE); ---To get the search running--- UINT ListFunc(LPVOID pParam) { CMyDlg* local = (CMyDlg*)pParam; local->BuildFileList(local->m_startDir); return 0; } ---To do the actual searching--- void CMyDlg::BuildFileList(CString startingDir) { chdir(startingDir); // start working for files CFileFind finder; BOOL bWorking = finder.FindFile(); int counter = 0; CStringArray pathArray; pathArray.SetSize(500,500); while (bWorking && running) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; if (finder.IsDirectory()) { CString str = finder.GetFilePath(); TRACE("str = %s\n",str); BuildFileList(str); } CString temp = finder.GetFilePath(); if(temp.Find(".wav",0)!= -1) { TRACE("Found wav file = %s\n",temp); temp.Delete(0,(m_startDir.GetLength()+1)); TRACE("wav file now is = %s\n",temp); pathArray[counter] = temp; } filePathArray[counter] = pathArray[counter]; counter++; } noOfFiles = counter+1; filePathArray.Copy(pathArray); <---- Heres the problem as I see it finder.Close(); }
The problem as far as I know is the filePathArray.Copy function near the end. The filePathArray is a CStringArray defined in the actual Dlg class that I want to store all of the details. But as it is, when the thread ends after a WaitForSingleObject(hThread,INFINITE); The filePathArray is empty :( Does anyone know where I'm going wrong with this thing before I go slightly loopy? At one point I had it copying a list of stuff I didn't want (a list of files in a dir, not .wav files) but I cannot get it to copy anything at all now. Thanks for looking lads :)Try replacing
Copy
withAppend
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Try replacing
Copy
withAppend
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollohehe, thanks Joaquin, again you're first to chip in an answer :) I'll give that a try as soon as I work out the bigger problem I just noticed. Its amazing what you can find out if you add small bits to a TRACE statement. Just found out that when it searches a new directory it resets the counter to 0 so I think I need to fix that first :)
-
hehe, thanks Joaquin, again you're first to chip in an answer :) I'll give that a try as soon as I work out the bigger problem I just noticed. Its amazing what you can find out if you add small bits to a TRACE statement. Just found out that when it searches a new directory it resets the counter to 0 so I think I need to fix that first :)