Click to See Complete Forum and Search --> : Worker Thread


FresherMind
December 23rd, 2004, 05:57 AM
Hi Guru,s

How i create Worker thread for long calculations without hang the application .
please send me code or related metirial .


thanks

Andreas Masur
December 23rd, 2004, 06:55 AM
Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312452)....

FresherMind
December 23rd, 2004, 08:01 AM
Hi
thanks for reply. I create a application (Dialog based ) which have tab control and the list control within this tab control . In Thread function i used
AfxBeginThread(ProgressThread, (LPVOID*)pMyListCtrl);
where CMyListCtrl *pMyListCtrl ;
but it fails . i called this thread form dialog button .

code as follows






BOOL CMyListCtrl::UpDateFontStatusIcon(CString str ,int flag)
{
CMyDlg *pMainDlg = (CMyDlg *)AfxGetMainWnd(); //main dailog

CLeftTabCtrl *pMyTab = (CLeftTabCtrl *)AfxGetMainWnd()->GetDlgItem( IDC_TABLEFT_MYDLG);

if( pMyTab != NULL)
{
for (int i = 0; i < pMyTab ->GetPageCount(); i++ )
{
CMyListCtrl *ptrTmp ;

ptrTmp = (CMyListCtrl *) pLeftTab->m_tabPages[i]->GetDlgItem(IDC_MYLIST); // here assertion fail

}
}



please help me

Marc G
December 23rd, 2004, 08:22 AM
MFC is not thread safe. Don't use MFC objects from 1 thread in another thread.
Use plain Win32 API for safety.

cody123456
December 23rd, 2004, 08:34 AM
Hi,
MFC is not thread safe.
How about ATL/WTL?

FresherMind
December 23rd, 2004, 08:38 AM
hi

how can i used Win32 API ?

Marc G
December 23rd, 2004, 08:42 AM
Instead of passing a pMyListCtrl (pointer to an MFC object), pass the HWND of the object. In your thread, don't use MFC method calls but send messages to the HWND.
For example: instead of pMyListCtrl->GetItemCount(), use SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0).