Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am Crating a thread in dialog based application where i am calling a function in the thread which should display the .jpg image to the static text control. but i am unable to display it on the static text control using thread. what is mistake i am doing in the below code,

void Fun()
{
CreateThread(0, 0, myThread, &myCounter, 0, NULL);
}

DWORD WINAPI myThread(LPVOID lpParameter)
{
unsigned int& myCounter = *((unsigned int*)lpParameter);
CRecognitionDlg* pDlg = (CRecognitionDlg*)lpParameter;
pDlg->PictureCount = 0;
pDlg->iCount = 0;
while(myCounter < 0xFFFFFFFF)
{
pDlg->ShowFacesForEnrollement();
if (pDlg->sFlag == FALSE)
{
break;
}
}

CloseHandle(myHandle);

return 0;
}
CPictureCtrl m_pFaceImages;
void dlg ShowFacesForEnrollement()
{
m_pFaceImages.Load(CString(_T("Normal.jpg")));
}

in the above code where i am doing wrong can anyone help me out.

Thanks in advance.
Posted

1 solution

Accessing GUI objects from a worker thread is not possible.
Here you created myThread and trying to access CPictureCtrl from it.
I suppose CPictureCtrl is a class to display an image in a static window.

If image loading consumes time, you can load it in a worker thread, after loading send a message to GUI to display it.
Normally non gui actions which consumes time are performed in a worker thread. After finishing operations, you can send a user message(which indicates GUI update after an action from worker thread) to GUI thread. GUI actions are performed from the message handler of new user message. Message handler for the user message will be executed in GUI thread.
 
Share this answer
 
v2
Comments
Kumar 09 27-Oct-12 0:46am    
any example
Santhosh G_ 27-Oct-12 0:57am    
http://www.codeproject.com/Answers/474330/timerplusandplusthreadplusinplusmfcplusdoesplusnot#answer2
Here non gui actions are performed from a worker thread and after that send message to gui.
Santhosh G_ 27-Oct-12 1:16am    
// Message declaration.
const int SET_GUI_UPDATE_MESSAGE = WM_USER + 1 // User Mesasge

// message handler modification.

// Add message handler in message map.
ON_MESSAGE( SET_GUI_UPDATE_MESSAGE, OnGUIUpdateMessage )


// Message handler implementation.
void MyDlgClass::OnGUIUpdateMessage( WPARAM dwWParam_i, LPARAM dwLParam_i)
{
// Process GUI operation..
}

// Non-GUI thread, sending message to GUI thread after non gui operations.
UINT MyThread()
{
// Non gui operations

...

// Send message to GUI for GUI actions
ptrDlgHandle->PostMessage( SET_GUI_UPDATE_MESSAGE, 0,0 );
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900