Thread: Multithread drawing problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Multithread drawing problem

    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^^

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well - this isn't a good use threads. The resource overhead of creating a new thread (and all the underlying MFC objects) will just slow things down even more.

    If you want to speed things up a little, you can cache your memory DC and just keep your bitmap selected to it. The idea is to reduce your "OnDraw" method to just a BitBlt. You may also be able to reduce flicker by handling WM_ERASEBKGND and simply returning FALSE.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Problem with drawing Yin Yang?
    By biosninja in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2002, 11:20 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM