Solved

std string value mysteriously changes.

Posted on 2004-09-15
8
165 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 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