Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Threading & CStringArray - long post warning

Threading & CStringArray - long post warning

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdebugginghelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    carrie
    wrote on last edited by
    #1

    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 :)

    J 1 Reply Last reply
    0
    • C carrie

      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 :)

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Try replacing Copy with Append. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      C 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Try replacing Copy with Append. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        C Offline
        C Offline
        carrie
        wrote on last edited by
        #3

        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 :)

        C 1 Reply Last reply
        0
        • C carrie

          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 :)

          C Offline
          C Offline
          carrie
          wrote on last edited by
          #4

          Just tried it, as well as fixing my counter messup that Append did the trick. Thanks again :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups