Solved

Query related to Multithreading?

Posted on 2006-11-09
12
180 Views
Last Modified: 2013-11-20
Hi

I am working on an MFC based SDI (Explorer type) application which searches for 2 headers (BMP & JPG) in the logical drive selected by the user.

The searching headers job i am doing in a UI Thread.

As soon as any header is found i add it to a Linked list maintained by me and then i send messages to the mainframe & leftview class to update the interface..

In the mainframe class in the message handler function i update the progress bar in the statusbar pane 0 & headers found count in the statusbar pane 1.

In the leftview message handler function i create a folder by the name of the header found (ex: BMP) & add it to the tree.

Now after half of the logical drive processed 2 folder exists in the left view - BMP & JPG.

So far so good.....................

Now when the user clicks on any folder(for ex: BMP) in the leftview, i will show the thumbnails of the various bmp's found so far by the searching process & stored in the linked list.

I have used a worker thread to show the thumbnails.

Now there are two problems related to the above discussion:

1-----> When the thumbnails are being shown by the worker thread, the header searching UI threads hangs untill all thumnails are displayed. Please have a look at the code for the worker thread:

 UINT      LoadingThumbnails_WorkerThread(LPVOID pParam)
{
      CSPhApp      *pApp = ( CSPhApp* )AfxGetApp();

      HWND      hHandle      =      (HWND)pParam;

      CWaitCursor      obj;

      while(pApp->pTemp)
      {
            if(pApp->pTemp->dwParentId      ==      pApp->dwCurrentlySelected_Dirs_FileId)
            {
                  BOOL      ret_Value = PostMessage(hHandle, WM_THUMBNAILS, 0, 0);

                  Sleep(500);
            }
            pApp->pTemp      =      pApp->pTemp->next;
      }//end of while..

      //When control comes here, loading of thumbnails is done.
      pApp->LoadingThumbNails      =      FALSE;            

      return 0;
}


Y is my UI thread hanging.

Waiting for suggestions

Regards
 















0
Comment
Question by:davinder101
  • 6
  • 4
  • 2
12 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17904817
When you execute a worker thread your UI thread might be in halt state / not receiving messages.......

I think you need to pump messages to your UI thread while you are waiting for execution of your worker thread......

Something like following ..depending on your situation you need to change.... just for rough idea its like :

 CUIThreadDlg dlg;
 ....
 dlg.Create(........);

 CWinThread * pThread = AfxBeginThread(transferthread, &ti, 0, CREATE_SUSPENDED, NULL);
 pThread->m_bAutoDelete = false;
 pThread->ResumeThread();

 while (WaitForSingleObject(pThread->m_hThread, 5) != WAIT_OBJECT_0)
{
  dlg.Pump();   // Pump Messages to UI Thread
 }
 dlg.Close();


BOOL CUIThreadDlg::Pump()
{
 MSG msg;

 // Retrieve and dispatch any waiting messages.
 while (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
  if (!AfxGetApp ()->PumpMessage ()) {
   ::PostQuitMessage (0);
   return FALSE;
  }
 }
 return TRUE;
}

-MAHESH
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17904945
Actually that thread does NOT display the thumbnails - all it does it instruct the main thread to do that, hence your blockage.  (I assume the hHandle is to the dialog in the main thread).

There is an MFC example - MTRECALC - which shows you how to update a window from a thread.
0
 

Author Comment

by:davinder101
ID: 17904975
Suppose there are 20,00,000(1 GB) sectors in the logical drive, & i m reading 64 sectors at a time & then process each sector in a loop 0-63.
Then again read next lot of 64 sectors.

In the status bar pane 2 i am constantly updating the sectors read as Reading 64 of 2000000 .. Reading sector 128 of 20000000 .. Reading sector 196 of this & so on.

So the UI is constantly updated. As soon as any header is found a dir is added to the left view.

As soon as the user clicks the folder worker thread is launched to load thumbnails in the right view.

The purpose of launching worker thread is that the UI thread is not hanged, but it hangs when thumbnail is added & then resumes & then haults again when secon thumbnail is loaded & so on.

I have tried ur suggestion but no change.

Waiting for suggestions

Regards


0
 

Author Comment

by:davinder101
ID: 17904998
Yes Andy you are write the thread instructs the main thread to display the thumbnails.
hHandle is handle of the mainframe.

From the articles that i have gone thru, the thread sends messages for GUI updation.

Waiting for suggestions

Regards

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17905038
Look at MTRECALC - (unless I remember incorrectly) that shows you how to update a window from a thread.

What you have done is create a thread whose job is to tell the mainframe to update itself !

0
 

Author Comment

by:davinder101
ID: 17905076
The UI thread instructs the main thread's main frame class  to update the status Bar Panes 0,1 & 2. It also intructs the main thread's LeftView class to update its tree as soon as any header is found.

While the worker thread is running it instructs the main thread's RightView class to load the thumbnail.

So how do i solve this problem?

Waiting for suggestions

Regards
 
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:mahesh1402
ID: 17905084
>>BOOL     ret_Value = PostMessage(hHandle, WM_THUMBNAILS, 0, 0);
>>  Sleep(500); <======

Don't use Sleep().  Use WaitForSingleObject or WaitForMultipleObjects instead.

[main thread]
hWakeUp = CreateEvent( NULL, TRUE, FALSE, NULL );
<init and launch work thread>
// For some reason you need to wake up the work thread
SetEvent( hWakeUp );

[work thread]
WaitForSingleObject( hWakeUp, INFINITE );
ResetEvent( hWakeUp );
DoSomething();

The OS ensures that threads waiting on WaitForXObject() take up no CPU time at all.  Using WaitForMultipleObjects is better .....

-MAHESH
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17905171
As I keep saying - load/display the thumbnail in the thread.  Have you looked at MTRECALC yet ?
0
 

Author Comment

by:davinder101
ID: 17905417
<<As I keep saying - load/display the thumbnail in the thread.>>

The thumbnail is to be loaded/displayed in the RightView(derived from CListCtrl) which belongs to the main thread.
So i thik the instruction has to be send to the main thread only.

Probably if i convert this worker thread into a UI thread & then create a dialog box from this thread & display thumbnail on it, the other UI thread will not hang.

Waiting for suggestions

Regards
 
0
 

Author Comment

by:davinder101
ID: 17913297
Hi
The points are increased.

Waiting for suggestions

Regards

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 350 total points
ID: 17913589
I've just checked, the correct sample is MTMDI (not MTRECALC).  Have a look at it.  
There you will see that the user interface is not cripppled as the window is being redrawn.  (Bounce window).
0
 

Author Comment

by:davinder101
ID: 17977838
Hi
Sorry for the delay.

Thanx
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video discusses moving either the default database or any database to a new volume.

747 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