Solved

std string value mysteriously changes.

Posted on 2004-09-15
8
163 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
Comment Utility
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
Comment Utility
Sounds good jkr
that could be the problem.
I'll try first thing tomorrow morning.
Thanks,
Jens
0
 
LVL 5

Author Comment

by:allmer
Comment Utility
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
Comment Utility
As long as you don't use the same storage space for all threads (as above), that's OK.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Author Comment

by:allmer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 125
Detect CR LF to each line 12 137
Hibernate methods 2 58
centeredAverage challenge 8 120
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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now