Click to See Complete Forum and Search --> : Quick CWinThread Question please


Phill Heald
December 3rd, 2006, 12:34 PM
Hi All...
Not being au fait with multi threading, Id just like to get your advice,
Im starting a thread like this:


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

Marc G
December 3rd, 2006, 01:18 PM
[ removed duplicate thread ]

[ Moved thread ]

Marc G
December 3rd, 2006, 01:24 PM
Just cast the returned pointer like:
m_pFileHub = (CFileHub*)AfxBeginThread(RUNTIME_CLASS(CFileHub),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);

wildfrog
December 3rd, 2006, 01:51 PM
Or use the C++ style static_cast operator:

m_pFileHub = static_cast<CFileHub*>(AfxBeginThread(RUNTIME_CLASS(CFileHub),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED));

- petter

Phill Heald
December 3rd, 2006, 02:28 PM
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:
m_pProgressDialog->Create(IDD_PROGRESS_DIALOG);
I get an ASSERTION in DLGCORE>CPP here:
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

MrViggy
December 4th, 2006, 03:46 PM
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/library/48xz4yz9(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