Hello, I'm trying to draw using CDC objects, each drawing action is done in a thread function. Let's show some code:
Code:
void CMyClass::Draw( CDC *pDCin )
{
m_pDC = pDCin;
AfxBeginThread( &CMyClass::draw_threaded, this );
// CMyClass::draw_threaded( this );
}
UINT __cdecl CMyClass::draw_threaded(LPVOID pparam)
{
....
CMyClass *pcaller = (CMyClass*)pparam;
CBitmap *pOldBmp;
if( !pcaller->m_bVisible ) return 0;
//waiting to be possible to access to object resources
pcaller->m_csDraw.Lock(); //critical section of this object
if( pcaller->m_pDC == NULL )
pcaller->m_pDC = CDC::FromHandle( GetDC( pcaller->m_hwndContainer ));
CDC DC, *pDC = &DC; //stack allocation
pDC->CreateCompatibleDC( pcaller->m_pDC );
pOldBmp = pDC->SelectObject( pcaller->m_pbmp );
p.CreatePen( PS_SOLID , 1 , COLOR_LABELVALS_BORDER );
pOldPen = pDC->SelectObject( &p );
//Drawing rect
b.CreateSolidBrush( BACK_COLOR );
pOldBrush = pDC->SelectObject( &b );
pDC->RoundRect( rpos , roundEllipse );
pDC->SelectObject( pOldPen );
p.DeleteObject();
//restoring DC objects
pDC->SetBkMode( prevMode );
pDC->SelectObject( pOldBrush );
b.DeleteObject();
pDC->SetTextColor( oldTextColor );
pDC->SetBkMode( prevMode );
//UPDATING SCREEN
pcaller->m_pDC->BitBlt( pcaller->m_rectPos.left,
pcaller->m_rectPos.top,
pcaller->m_rectPos.Width(),
pcaller->m_rectPos.Height() ,
pDC, 0 , 0, SRCCOPY);
pDC->SelectObject( pOldBmp );
pDC->DeleteDC();
pcaller->m_csDraw.Unlock();
return 0;
}
Now, when I call the function as a thread:
Code:
AFXBeginThread( &CLinkedLabelVal::draw_threaded, this );
after a first call, the subsequent call causes an assert error:
Code:
MyApp: File afxwin1.inl, Line 598
I took a look to that file and it seems that the m_hDC of the bitmap is null....and this is really strange because I never delete the bitmap, it is a class member of the calling object.
This doesn't happen if I call the draw function in normal way, without using it in a seprarate thread ( using the commented line in the Draw() function):
Code:
CMyClass::draw_threaded( this );
Can somebody give me any hint? Thank you all for help^^