problem accessing dialog controls in UI thread

Discussion in 'MFC' started by Senthil, Feb 5, 2009.

  1. Senthil

    Senthil New Member

    Joined:
    Jan 22, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi!

    I have just got stuck in a thread dilemma.Here is my code.

    CUIThread.h
    Code:
     
    #pragma once
    #include "resource.h"
    
    
    // CUIThread
    
    class CUIThread : public CWinThread
    {
    	DECLARE_DYNCREATE(CUIThread)
    
    public:
    	CUIThread();           // protected constructor used by dynamic creation
    	virtual ~CUIThread();
        HWND m_hWnd;
    
    public:
    	virtual BOOL InitInstance();
    	virtual int ExitInstance();
    
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    
    ---------------------------------------------------------------------
    
    CUIThread.cpp
    
    // UIThread.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "UIThread.h"
    #include "resource.h"
    
    // CUIThread
    
    IMPLEMENT_DYNCREATE(CUIThread, CWinThread)
    
    CUIThread::CUIThread()
    {
    }
    
    CUIThread::~CUIThread()
    {
    }
    
    BOOL CUIThread::InitInstance()
    {
    	CDialog *ProBar = new CDialog();
        ProBar->Create(IDD_PROGRESS);
        ProBar->ShowWindow(SW_SHOW);
        ProBar->UpdateWindow(); 
        m_bAutoDelete = FALSE;    
        m_pMainWnd = ProBar;
        m_hWnd = m_pMainWnd->m_hWnd; 
    
        return TRUE;
    }
    
    int CUIThread::ExitInstance()
    {
    	return CWinThread::ExitInstance();
    }
    
    BEGIN_MESSAGE_MAP(CUIThread, CWinThread)
    END_MESSAGE_MAP()
    
    ------------------------------------------------------------------

    main.cpp


    CUIThread *ProDlg = ( CUIThread * )AfxBeginThread(RUNTIME_CLASS( CUIThread ) );



    The Modeless dialog is displayed but the problem is I am unable to access any controls in the modeless dialog from main.cpp

    can anyone tell where I have gone wrong or am I missing anything or is there any work around.

    It is a progress control dialog

    Thanks in advance

    :confused:
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You are totally wrong in the concept.

    You are creating a dialog in one thread and trying to access the control in other thread which would make your life miserable
     
  3. Senthil

    Senthil New Member

    Joined:
    Jan 22, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    But the dialog (progress control) has to be present in a separate thread

    Is there any good example code where I can refer in terms of thread synchronization
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Its not about synchronization. Windows control does not allow creation of control in one thread and update from other threads you need to use the same thread to update it. Try sending messages to the creation thread to update the controls
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice