Click to See Complete Forum and Search --> : Making a thread out of a method
DeepT
December 14th, 2004, 04:17 PM
Take the following code as an example:
UINT CBulkKeyTestDlg::SimThread(void *ptr)
{
}
AfxBeginThread(SimThread, NULL);
the compile error I get:
error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
Now If Simthread is removed as a member function and just simply delcared (and removed from the object header) it compiles just fine.
How do I get this function to be acceptable as a thread as a member function? The guts of the function has to have access to a bunch of private members of the class so I can't simply delcare it and use it.
GCDEF
December 14th, 2004, 04:25 PM
I don't believe your function can be a member of a class.
DeepT
December 14th, 2004, 04:26 PM
It has to be. If not, how can I look at class variables?
GCDEF
December 14th, 2004, 04:32 PM
Pass it a pointer to an object that contains the variables you want to see.
Jim Mcpherson
December 14th, 2004, 04:36 PM
// In your header file
static UINT SimThread(LPVOID ptr);
//
// In your cpp file
UINT CBulkKeyTestDlg::SimThread(LPVOID ptr)
{
CBulkKeyTestDlg* pDlg = ( CBulkKeyTestDlg*)ptr;
}
/
AfxBeginThread(SimThread, this);
Zim327
December 14th, 2004, 04:53 PM
worker threads can be either global or member functions
there are plenty of articles on CG that tell you how to do this, just search for worker thread and/or GUI thread...
the worker thread has no "this" pointer so any vital data must be passed into it using the void pointer.
For example, just load all of your data into a struct and cast it to a void pointer
then inside the worker thread you'll have to cast it back to the struct, just like the way Jim demonstrated.
If the thread has to update resources outside of it then just use PostMessage
there are plenty of CG articles on this subject too.
HTH,
DeepT
December 14th, 2004, 05:02 PM
Well i got it to compile looking at some examples, but now I have a memory access violation problem. here is the code so far:
UINT CBulkKeyTestDlg::SimThread(void *ptr)
{
CBulkKeyTestDlg *me = (CBulkKeyTestDlg *)ptr;
me->SimThread();
return 0;
}
void CBulkKeyTestDlg::SimThread()
{
POSITION Pos;
// Collect all the Package Answers
Pos = PKCTList.GetHeadPosition(); <<-- Access violation
}
Infact accessing any member variable, even bools or ints cause that error during run time. Ideas?
Zim327
December 14th, 2004, 05:09 PM
in your worker thread try using dynamic_cast or static_cast rather than the old C-style cast
DeepT
December 14th, 2004, 05:12 PM
and that will clean up my memory access violation error?
Zim327
December 14th, 2004, 05:14 PM
perhaps, if that doesn't work then I would suggest using the new operator
oh and use an ASSERT to see if your pointer is not null
Zim327
December 14th, 2004, 05:21 PM
I'm sorry, what is PKCTList? if its a member of your dlg then you'll have to do this:
me->PKCTList.GetHeadPostion();
remember your thread exists separately from the primary thread, so most anything you do will start with me->
DeepT
December 14th, 2004, 05:22 PM
New? Umm I am not sure you understand what I am trying to do.
I have Class X. I want to write a function/method of Class X that can be launched as a thread and have full access to Class X's private member variables.
Zim327
December 14th, 2004, 05:25 PM
My apologies, see post#11
DeepT
December 14th, 2004, 05:27 PM
PKCTList is a private member of CBulkKeyTestDlg. At any rate accessing ANY members this problem, even a bool or int. There is no 'me' in the function (without the void pointer) and even using this->memberVariable, causes the same memory access violation.
DeepT
December 14th, 2004, 05:32 PM
Ok everyone, I am an idiot. I found my problem.
What is wrong with this call?
AfxBeginThread(SimThread, NULL);
yea that was the problem, it should have been:
AfxBeginThread(SimThread, this); // DOH!
Thanks for your help though, however I didnt know the technique of static casting it and all that. I didnt know it was called a 'worker thread' so couldnt search on it.
Zim327
December 14th, 2004, 05:40 PM
I'm pretty sure I understand what you're trying to do:
First of all, There is no this pointer in a worker thread.
You must pass all vital data and resources into the worker thread and use them everwhere. there are exceptions, like you can't pass a CTreeCtrl variable into a worker thread.
You should be able to pass a ptr to a dialog window though...
can you post the code where you actually call the thread function?
Your SimThread will have to be either be placed inside your worker thread or you'll have to communicate with the primary thread by creating your own messages and send them using PostMessage from within the thread.
Andreas Masur
December 14th, 2004, 06:08 PM
[ Moved thread ]
Andreas Masur
December 14th, 2004, 06:13 PM
How do I get this function to be acceptable as a thread as a member function?
The answer can be found in the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312453)...
Andreas Masur
December 14th, 2004, 06:15 PM
I don't believe your function can be a member of a class.
Yes...it can...see my previous reply... :cool:
Andreas Masur
December 14th, 2004, 06:22 PM
and that will clean up my memory access violation error?
Nope...although you should prefer the 'static_cast' (and not the 'dynamic_cast') over the old-style C cast, does not change anything in reagrd to an access violation...
Andreas Masur
December 14th, 2004, 06:26 PM
there are exceptions, like you can't pass a CTreeCtrl variable into a worker thread.
Well...you can pass a 'CTreeCtrl' into the worker thread...however, that would not help much since you cannot do anything with it... :D
You should be able to pass a ptr to a dialog window though...Yes, you are...
Andreas Masur
December 14th, 2004, 06:27 PM
And finally...if you want to access GUI elements (such as the 'CTreeCtrl') from within a worker thread....take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?t=312454)...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.