Solved

std string value mysteriously changes.

Posted on 2004-09-15
8
164 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now