Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread-Programming

Status
Not open for further replies.

AMosmann

Programmer
May 27, 2003
82
DE
I tried to use the following:

UINT XX::YY(LPVOID pParam)
UINT TestThread(LPVOID pParam)

...

CWinThread *pThread=::AfxBeginThread(XX::YY, GetSafeHwnd(),THREAD_PRIORITY_NORMAL);

CWinThread *test=AfxBeginThread(TestThread,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);

the second version starts, the first is not to be compiled, errormessage is

error C2665: 'AfxBeginThread' : Durch keine der 2 Ueberladungen kann Parameter 1 vom Typ 'unsigned int (void *)' konvertiert werden

(can not find matches between 'unsigned int (void *)' and the function 'AfxBeginThread'

I tried to cast explicit, but the result was the same.

Is it possible to declare and implement a object method as a thread?

Thanx
 
No you can't use member functions as a thread proc.

It's a limitation of the windows API, which doesn't know how to handle the "This" pointer that preceeds the address of the function, when it's a member function.

If you want your object to be used, you can send in a pointer as the LPARAM parameter to it though...



Greetings,
Rick
 
Not strictly true,
you can use members as thread procedures if you do a little fiddling first...
//this is the member you want to use as yer thread
UINT ThreadProc();

//this is the proc u pass to AfxBeginThread
static UINT TestThread(LPVOID pParam)
{
CTestDlg* PsudoThis = (CTestDlg*)pParam;
return PsudoThis->ThreadProc();
};

///this is how u start the thread form inside yer object
AfxBeginThread(TestThread, this,THREAD_PRIORITY_NORMAL);
 
Thanx, but as far as I understood you said the same as LazyMe recommended.

You give the pointer to the object to a simple function and inside this you convert it to the object it represents.
Then you can use the object method from the simple function.

This way I tried, it works.
Thanks to both of you.

(Sorry for my English)
 
>>static UINT TestThread(LPVOID pParam)

Static functions are not considered member functions, as they can't operate on member variables, can't call member functions etc. without a pointer to a particular instance.



Greetings,
Rick
 
Point taken, they cannot access non static members but I class them as member functions because, unlike global static functions they retain their class level access in such that private, protected static members are not accessible outside of an instance of the class they are members of. This does not apply to static member variables.
Regardless of this, using a thread running in the above manner to receive or update dialog members becomes problematic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top