Passing cdc across threads
-
Hi pals, I am facing a difficulty with CDC across threads. Actually, i spawn a secondary thread to calculate the display coordinates of the strings. So i am calculating the height and width of the strings based on the font selected in the CDC. It works fine with the scroll view but the calculation goes wrong with the PrintPreview. Bcos ,SelectObject(pMyFont) is always returning NULL. How do i solve this problem. Help me out friends. Thanks in advance. Kumari
-
Hi pals, I am facing a difficulty with CDC across threads. Actually, i spawn a secondary thread to calculate the display coordinates of the strings. So i am calculating the height and width of the strings based on the font selected in the CDC. It works fine with the scroll view but the calculation goes wrong with the PrintPreview. Bcos ,SelectObject(pMyFont) is always returning NULL. How do i solve this problem. Help me out friends. Thanks in advance. Kumari
MFC objects do not operate well across threads. There are some Knowledge base articles on MSDN that explain why. Basically they use thread local storage to map the resource handle to the C++ object pointer. So in another thread the mapping does not exist. So always pass the resource HANDLE rather than an MFC C++ class pointer across threads. then if you want to use MFC classes in the thread you can attach the HANDLE to a thread local scope MFC object.
"No matter where you go, there your are." - Buckaroo Banzai
-pete
-
Hi pals, I am facing a difficulty with CDC across threads. Actually, i spawn a secondary thread to calculate the display coordinates of the strings. So i am calculating the height and width of the strings based on the font selected in the CDC. It works fine with the scroll view but the calculation goes wrong with the PrintPreview. Bcos ,SelectObject(pMyFont) is always returning NULL. How do i solve this problem. Help me out friends. Thanks in advance. Kumari
-
Pass the HDC. Make sure the CDC is retreived by GetDC. Make thet window class has CS_OWNDC. you get it on WM_CREATE. you can set the GDI objects into this context once and thay persistent couse to CS_OWNDC.
-
Pass the HDC. Make sure the CDC is retreived by GetDC. Make thet window class has CS_OWNDC. you get it on WM_CREATE. you can set the GDI objects into this context once and thay persistent couse to CS_OWNDC.
Hi suiram Thanks for your response. Yes i am passing hdc to the thread. I tried with CS_OWNDC, But of no improvement. My problem is with the CDC of PrintPreview. What should i do now. Please, do help me. kumari