Link to home
Start Free TrialLog in
Avatar of davinder101
davinder101

asked on

Query related to Multithreading?

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
 















Avatar of mahesh1402
mahesh1402
Flag of India image

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
Avatar of AndyAinscow
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.
Avatar of davinder101
davinder101

ASKER

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


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

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 !

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
 
>>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
As I keep saying - load/display the thumbnail in the thread.  Have you looked at MTRECALC yet ?
<<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
 
Hi
The points are increased.

Waiting for suggestions

Regards

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi
Sorry for the delay.

Thanx