Solved

std string value mysteriously changes.

Posted on 2004-09-15
8
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 59
sum67 challenge 35 130
Modbus - whats the maximum I can store in one register? 4 85
Unix Command -- Challenging  question 7 107
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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