File checking progress

I'm trying to do a file browser, that search .dcm files in a directory. I would like to see in a edit box on my dialog each file scanned by the browser while it is searching. But when I start the browse, it's impossible to do anything in the dialog, and so the edit box I try to fill remains empty until the search is finish. I also would like to let the user press the Abort button of my dialog while the program is searching. How can I do that?
mruelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
The root of the problem is that your application is not proccessing mesages while it is browsing.  i.e you are not running a message loop so message like WM_PAINT are not being processed so the edit box does not change its contents.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mruelAuthor Commented:
And how can a process this message loop?
Is it with PreTranslateMessage?
0
nietodCommented:
There are two approachs to solving this problem.

One approach is to use multiple threads.  This is more work and more complex than the other, It requires more care in programming., but in complex cases it has the best behavior.  In this case the first thread, would start a new thread to do the searching.  Then the thread would return to processing messages.  The edit box would redisplay when its contents are changed.  The search thread could then be aborted by the 1st thread, using a semaphore of some sort.


the second approach is much easier to program.  it works very well in simple cases, but if algorithms and things get complex, the multiple thread solution is better.   This solution is to just use 1 thread and simply process messages periodically during the thread.   The way this works is you do a small task, like process 1 file, then you run a short message "peek" loop that gets and processes any available window messages.  When there are no message left to be processed, the code repeats, i.e. it processes the next file and then looks for messages again.  As on as the time between handling messages is short, this will work well.


Let me know if you have any questions.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

nietodCommented:
Are you progamming in MFC?
0
mruelAuthor Commented:
Yes. I'm using a CDialog object.
0
mruelAuthor Commented:
Yes. I'm using a CDialog object.
0
nietodCommented:
In MFC, I think you would be best of if you start an new thread--a worker thread.  Are you familiar with multi-threaded programming?
0
mruelAuthor Commented:
No, but I'm learning on the subject now...
0
mruelAuthor Commented:
Now I can start my thread one time and it works. But when I click another time on my update button (which start the file search, and normally, the thread) the thread doesn't works. Why is it working only one time?
0
nietodCommented:
I couldn't say with what little info you presented.  Can you provide other details?  code?
0
mruelAuthor Commented:
I wasn't sure you would answer, so I waited for you to answer before posting code...

In OnButtonUpdate(), I call FindInSecondThread();

In FindInSecondThread(), I initialise a struct and pass it to AfxBeginThread :

if (m_pWorkerThread == NULL)
{
// Begin the worker thread.
m_pWorkerThread =
AfxBeginThread(ThreadFindFilesProc, &m_ThreadInfo);
}

In my ThreadProc :

UINT ThreadFindFilesProc(LPVOID pParam)
{
CThreadFindFilesInfo* pInfo = (CThreadFindFilesInfo*)pParam;

FindFilesRecurse(pInfo->m_info_currentDir, pInfo->m_info_nb_images_found, pInfo);

::PostMessage(pInfo->m_hwndNotifyFindDone,
            WM_USER_FIND_DONE, 0, 0);
return 0;
}
 
In FindFilesRecurse, that I call recursively, I browse the current directory and when I found I .dcm file, I do a postmessage to the first thread:

::PostMessage(pInfo->m_hwndNotifyFindDone,
      WM_USER_FIND_IN_PROGRESS, 0, 0);

to indicate the dialog must print the path of the directory scanned.

And when I do it one time, it's okay. If I press another time on Update, it does the same thing as if I had only one thread... And when I change the current directory, it works again... I don't know what's the problem.
0
nietodCommented:
How is the "&m_ThreadInfo" that is passed to the managed?  What I mean is how is it declared and used?

If it is a local varaible, it will be destroyed when the procedure that declared it ends.  This may occur BEFORE the new thread uses it.  So the new thread will obtain garbage instead of the propper parameters.  antoehr posibile problem is that the new main thread may have a single copy of the data and it may be altering the data before creating the new thread.  So in this case all the threads created may be sharing this one copy of the data that is changing.  Does that make sense?  could those sorts of problems be happening?

One solution is to dynamaclly allocate the parameter data  (with new) and pass the pointer to the new thread.  The new thread will be responsible for deleting the data (with delete) when it no longer needs it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.