PDA

View Full Version : مشکل در ایجاد Thread



menevlster
دوشنبه 22 فروردین 1390, 21:31 عصر
سلام
چرا من در Thread زیر نمی توانم از m_listctrl استفاده کنم
کد من:


HANDLE m_hThread;
CListCtrl m_listctrl;
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
CListCtrl *mf=(CListCtrl *)lpParam;
در این مرحله برنامه قفل می کند int nItem = mf->GetItemCount(); //
خطا اینست: { ASSERT(::IsWindow(m_hWnd));
return 0;
}
void DoThread(CListCtrl & m_listctrl)
{
if(!m_hThread)m_hThread = CreateThread(NULL, 0,ThreadProc,(LPVOID)m_listctrl,0,NULL);

WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread);
}


البته این سئوال را از msdn پرسیدم و یک جواب این بود:


Change this line:
if(!m_hThread)m_hThread = CreateThread(NULL, 0,ThreadProc,(LPVOID)m_listctrl,0,NULL);
To this:
if(!m_hThread)m_hThread = CreateThread(NULL, 0,ThreadProc,(LPVOID) &m_listctrl,0,NULL);



این راه حل جواب نداد.


جواب دوم اینست:
You have created a deadlock. WaitForSingleObject suspends the main thread, but the main thread is the only thread that can access the control.
Access the list control only from the thread that created it. Your worker thread can post messages to the GUI thread to ask it to do the things you need to controls. See FAQ 11 & 12 here:



لطفا راهنایی کنید.

menevlster
دوشنبه 22 فروردین 1390, 23:39 عصر
مدیران محترم لطفا جواب دهید
ضمنا در رابطه با سئوال قبلی آیا کد زیر در ThreadProc می تواند موثر واقع شود؟
SendMessage(hwnd, MY_MESSAGE, (WPARAM)0, (LPARAM)0);

سئوال اینکه hwnd را چگونه به ThreadProc انتقال می دهیم؟