CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Question Quick CWinThread Question please

    Hi All...
    Not being au fait with multi threading, Id just like to get your advice,
    Im starting a thread like this:


    Code:
    	m_pFileHub = AfxBeginThread(RUNTIME_CLASS(CFileHub),
                                        THREAD_PRIORITY_NORMAL,
                                        0, 
                                        CREATE_SUSPENDED);
    where m_pFileHub is a pointer to a class derived from CWinThread.
    Now the compiler doesnt like it because it insists on returning a CWinThread*, not a CFileHub*.

    My question is, how do I start my thread then get access to its variables and methods. I had thought I would be able to use the pointer returned by AfxBeginThread !

    Thanks all for helping me
    Phill

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Quick CWinThread Question please

    [ removed duplicate thread ]

    [ Moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Quick CWinThread Question please

    Just cast the returned pointer like:
    Code:
    	m_pFileHub = (CFileHub*)AfxBeginThread(RUNTIME_CLASS(CFileHub),
                                        THREAD_PRIORITY_NORMAL,
                                        0, 
                                        CREATE_SUSPENDED);
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Quick CWinThread Question please

    Or use the C++ style static_cast operator:
    Code:
    m_pFileHub = static_cast<CFileHub*>(AfxBeginThread(RUNTIME_CLASS(CFileHub),
                                        THREAD_PRIORITY_NORMAL,
                                        0, 
                                        CREATE_SUSPENDED));
    - petter

  5. #5
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: Quick CWinThread Question please

    Thanks Guys !
    Wildfrog, like the solution.. neat !

    Incidently Im trying to call ::Create against a CDialog* in a function in the Threaded class.
    The compiler is having non of it.

    during this call:
    Code:
    m_pProgressDialog->Create(IDD_PROGRESS_DIALOG);
    I get an ASSERTION in DLGCORE>CPP here:
    Code:
    BOOL CDialog::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
    {
    	ASSERT(HIWORD(lpszTemplateName) == 0 ||
    		AfxIsValidString(lpszTemplateName));
    
    --> -->	m_lpszTemplateName = lpszTemplateName;  // used for help
    	if (HIWORD(m_lpszTemplateName) == 0 && m_nIDHelp == 0)
    		m_nIDHelp = LOWORD((DWORD)m_lpszTemplateName);
    
    #ifdef _DEBUG
    	if (!_AfxCheckDialogTemplate(lpszTemplateName, FALSE))


    Is there any reason why I cant do this in a class derived from CWinThread ?

    Thanks
    Phil
    Last edited by Phill Heald; December 3rd, 2006 at 03:59 PM.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Quick CWinThread Question please

    Did you create it as a "GUI" type thread? There are two types of threads worker threads, and GUI threads:

    http://msdn2.microsoft.com/en-us/lib...z9(VS.71).aspx

    There's an example project on that page that might help you out (I haven't looked at the example myself).

    Viggy

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured