dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 30

Thread: static drawing in Worker Thread

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    93

    static drawing in Worker Thread

    i got this error when i try to run the pogram, how to solve the error?



    i have make the file to rar format so that is easy for access it.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: static drawing in Worker Thread

    You are breaking the fundemental rule that says you can't access MFC UI components from a thread other than the thread that created the UI components.

    Usually you'll want to put all your UI components in the main UI thread, but if you absolutely can't live with this, check out the Multithreading: Creating User-Interface Threads article in Msdn.

  3. #3
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote Originally Posted by Arjay
    You are breaking the fundemental rule that says you can't access MFC UI components from a thread other than the thread that created the UI components.

    Usually you'll want to put all your UI components in the main UI thread, but if you absolutely can't live with this, check out the Multithreading: Creating User-Interface Threads article in Msdn.

    ??? basic user of visual c++, can explain in much simpler term?
    Last edited by ashwarrior; May 20th, 2008 at 12:43 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: static drawing in Worker Thread

    Quote Originally Posted by ashwarrior
    ??? basic user of visual c++, can explain in much simpler term?
    These MFC UI objects are created in the main UI thread:
    Code:
    	CDC		memdcDraw;
    	CBitmap bmDraw;
    but you are accessing them in your second thread:
    Code:
    // Secondary thread proc
    UINT CMy123Dlg::ABC(LPVOID pABC)
    {
    	CMy123Dlg*Draw=(CMy123Dlg*)pABC;
    	Draw->Test();
    	return 0;
    }
    
    void CMy123Dlg::Test()
    {
    	CBrush WhiteBrush(RGB(0,0,0));
    	memdcDraw.SelectObject(&WhiteBrush);
    	memdcDraw.SetBkMode(TRANSPARENT);
    	CPen WhitePen(PS_SOLID, 2, RGB(255,255,255));
    	memdcDraw.SelectObject(&WhitePen);
    
    	CFont myFont;
    	memdcDraw.SelectObject(&myFont);
    	memdcDraw.SetTextColor(RGB(255,255,255));
    
    	memdcDraw.Rectangle(50,50,100,100);
    	memdcDraw.TextOut(10,10,"hello");
    
        CClientDC dc(this);
    	dc.BitBlt(0,0,1100,800,&memdcDraw,0,0,SRCCOPY);
    }
    You aren't allowed to do that.

  5. #5
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    oh i only have 1 thread, the function Test() is not a thread.

    UINT CMy123Dlg::ABC(LPVOID pABC)
    {
    CMy123Dlg*Draw=(CMy123Dlg*)pABC;
    Draw->Test();
    return 0;
    }

    this is the only thread i have inside the whole program.

  6. #6
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    // Secondary thread proc
    UINT CMy123Dlg::ABC(LPVOID pABC)
    {
    CMy123Dlg*Draw=(CMy123Dlg*)pABC;
    Draw->Test();
    return 0;
    }

    void CMy123Dlg::Test()
    {

    }

    when i build the program with this code i got no errors and can run.
    so maybe is the the static drawing statement causing all the problem?.

  7. #7
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: static drawing in Worker Thread

    Quote Originally Posted by ashwarrior
    oh i only have 1 thread, the function Test() is not a thread.

    UINT CMy123Dlg::ABC(LPVOID pABC)
    {
    CMy123Dlg*Draw=(CMy123Dlg*)pABC;
    Draw->Test();
    return 0;
    }

    this is the only thread i have inside the whole program.
    No, there are 2 threads: the main UI thread and the worker thread serviced by the UINT CMy123Dlg::ABC(LPVOID pABC) function. As previously stated you cannot do this. Your worker thread should send requests ( messages) to the main thread to perform any UI operations.
    Har Har

  8. #8
    Join Date
    Apr 2008
    Posts
    93

    Re: static drawing in Worker Thread

    Quote Originally Posted by Arjay
    You are breaking the fundemental rule that says you can't access MFC UI components from a thread other than the thread that created the UI components.

    Usually you'll want to put all your UI components in the main UI thread, but if you absolutely can't live with this, check out the Multithreading: Creating User-Interface Threads article in Msdn.

    btw im using Worker thread instead of User interface thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)