Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

std string value mysteriously changes.

Posted on 2004-09-15
8
Medium Priority
?
170 Views
Last Modified: 2013-11-20
Hi experts,
I have a list of CString file paths and would like to
process them one by one. But when I start the thread
the value mysteriously changes to nothing.
Some code:
      if(pidl){
            ::SHGetPathFromIDList(pidl, szDir);
            dir = CString(szDir);
            SetFolderList(GetFolderList(dir));
            for(list<CString>::iterator i=databaseList.begin(); i!=databaseList.end(); i++) {
                  options.PATHTODATABASE = (*i).GetString();
               //at this point  options.PATHTODATABASE contains for example "c:\database\chlre2.fasta"
                  AfxBeginThread(BatchThread,this,THREAD_PRIORITY_BELOW_NORMAL,0);
            }
      }
      UpdateAllViews(NULL);


UINT CMainDoc::BatchThread(LPVOID pParam)
{
      CString path;
      size_t pos1,pos2;
      string temp,file;
      CMainDoc *pDoc = (CMainDoc*)pParam;
     //At this point the value of options.PATHTODATABASE is empty.
      pDoc->options.PATHTODATABASE = CString("c:\\database\\chlre2.fasta").GetString();
      CFileFind finder;
      string dir;
      if(pDoc->dir.GetLength() > 1) {
            dir = pDoc->dir.GetString();
            pDoc->dir += "\\*.*";
            BOOL bWorking = finder.FindFile(pDoc->dir);
            while(bWorking) {      

I am really lost at this point.
Any ideas?
Cheers,
Jens
0
Comment
Question by:allmer
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12069283
It seems that your path variable gets overwritten (remember, you are starting multiple threads and are using the same doc). Try

// somewhere in a header file
struct ThreadOptions {

    CString strPathToDatabase;
    CString strDir;

    // basically everything you need in the threads
};


     if(pidl){
         ::SHGetPathFromIDList(pidl, szDir);
         dir = CString(szDir);
         SetFolderList(GetFolderList(dir));
         for(list<CString>::iterator i=databaseList.begin(); i!=databaseList.end(); i++) {

              ThreadOptions* pOpt = new ThreadOptions;

              pOpt->strPathToDatabase = (*i).GetString();

              // etc.

              AfxBeginThread(BatchThread,pOpt,THREAD_PRIORITY_BELOW_NORMAL,0);
         }
    }
    UpdateAllViews(NULL);


UINT CMainDoc::BatchThread(LPVOID pParam)
{
    CString path;
    size_t pos1,pos2;
    string temp,file;
    ThreadOptions *pOpt = (ThreadOptions*)pParam;
 
    //... at the end of the thread, use 'delete pOpt;'
0
 
LVL 5

Author Comment

by:allmer
ID: 12069423
Sounds good jkr
that could be the problem.
I'll try first thing tomorrow morning.
Thanks,
Jens
0
 
LVL 5

Author Comment

by:allmer
ID: 12075303
I am passing the this pointer to the thread
and I guess I also need it (progress bars, data display, ..). Instead of rewriting
the whole thing would it be ok to make the
COptions options //in maindoc.h
a pointer so I can new the options for each thread?
Best,
Jens
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 12075514
As long as you don't use the same storage space for all threads (as above), that's OK.
0
 
LVL 5

Author Comment

by:allmer
ID: 12086012
Can I Only start one thread at a time and wait for it to finish?
I don't really want them to execute in parallel.
That would probably also solve the problem.
Cheers,
Jens
0
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 12086107
Sure you can. All you need to do is using the thread handle:

CWinThread* p = AfxBeginThread(BatchThread,pOpt,THREAD_PRIORITY_BELOW_NORMAL,0);

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &p->m_hThread,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }
0
 
LVL 5

Author Comment

by:allmer
ID: 12231038
Sorry jkr
that I get back to you this late :(
can I simply call
SleepEx(3000,false);
instead of the second while loop?
I don't have any msg var. And I don't control my own messages.
Too much work for a little biologist ;)
Thanks for your help,
Jens
0
 
LVL 86

Expert Comment

by:jkr
ID: 12244436
>>can I simply call
>>SleepEx(3000,false);

No, that won't fix the problem unless you *can* ensure that the job is *really* done within these 3kms...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question