Click to See Complete Forum and Search --> : I'm having problems with new thread and static member functions...
canus
June 30th, 2005, 08:29 AM
Hey everyone, this is my first post, woohoo! Now on to more interesting matters...
I'm trying to create a new thread that will continuously check the mouse position and determine whether or not it is in a specific rectangle, and if so do some processing. This thread needs access to some variables in the class that creates it, so I wrapped the function in a new class with some needed variables set when the object is created. Problem is, the function has to be static in order for _beginthread() to work. But when I make it static then all the member variables have to be static, and then I can't access non-static Windows functions, apparently, like MapWindowPoints().
Any ideas on how I could solve this problem?
Thanks for the help!
Hobson
June 30th, 2005, 08:40 AM
Create your member function as static, but keep fields of your structure non-static. when you start a thread, you can specify an address of its main function and an address to some memory space (LPVOID) containing data for this function. So, you can pass an address of object of your class. Inside of thread function cast it back to object of your class and access non-static fields of it.
Hob
PS. search the google and forum, it was mentioned a lot of times before.
retry
June 30th, 2005, 08:50 AM
Hobson is right, you need the thread function to be static but don't make all your data members static. instead pass the class pointer (this) to the thread function and access the data members through it.
cilu
June 30th, 2005, 08:51 AM
But when I make it static then all the member variables have to be static, and then I can't access non-static Windows functions, apparently, like MapWindowPoints().
No, they don't have. But as Hobson said, when you create the thread specify pointer this to be used as parameter (LPVOID) for the thread function. Then, in the function, cast back from LPVOID to your class type.
UINT SomeClass::TheThreadFunction(LPVOID lpParam)
{
SomeClass *pObj = (SomeClass *)lpParam;
pObj->foo(); // call whatever you want...
}
canus
June 30th, 2005, 09:03 AM
Thanks so much for the very helpful, and really quick, replies.
While I was trying to solve the problem I came across something that I was confused about that was related. (I was going to just create a regular function in my .cpp file and then create the thread with it. I created global variables for it, and I was going to try and bypass the static problem.) But when I tried calling CWnd::MapWindowPoints() it told me the same error again. "illegal call of non-static member function" But the calling function wasn't declared as static so why is there a problem? Or is the calling function automatically static since it is not part of a class.
I am feeling really dumb this morning.
By the way, I tried a quick search on the first topic I posted. I didn't see anything at first glance and shoulda looked harder. Sorry to post a redundant question. But thanks for helping me.
EDIT:: About the MapWindowPoints() function. There is more than one prototype for the function. The one I want to use is
void MapWindowPoints(
CWnd* pwndTo,
LPPOINT lpPoint,
UINT nCount
) const;
It's used previously in the code I'm working on(written by someone else). They don't prefix it with anything and it works. But when I try to use it without using CWnd:: it tells me that the function doesn't take 3 arguments. And then when I put the CWnd:: on the front it tells me the non-static call error. I don't get it.
SECOND EDIT: It probably has something to do with the fact that the class it is used in is based on CDialog which is based on CWnd. I'm having a very slow morning... But I still don't know why it tells me the whole non-static thing.
Arjay
June 30th, 2005, 11:18 AM
First of all, are you trying to call MapWindowPoints from inside your secondary thread?
Next, when working with MFC there are about four types of functions (or methods):
1) Non static class methods
2) Static class methods
3) MFC global functions
4) Windows api's
For non static class methods you need to call them via their instance variable:
pWnd->MapWindowPoints(...);
For static methods call them via the class scoping operator:
CMyClass::StaticMethod(...);
The MFC global function are usually prefixed by Afx and are called as follows:
CMainFrame* pMainFrame = AfxGetMainWnd();
Windows api's are called directly. To avoid ambiguity between MFC class methods, use the scoping operator. NOTE: Many api's take an additional parameter than the MFC counterparts because they require a handle of some sort. The MFC api equivalents typically store this handle in the class instance and as such don't require the param being passed to the method.
::MapWindowPoints(hWnd, ...); //NOTE: the :: scoping and extra param
Arjay
Andreas Masur
June 30th, 2005, 12:28 PM
[ Redirected thread ]
Andreas Masur
June 30th, 2005, 12:28 PM
MSDN:
Multithreading Topics (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_multithreading_topics.asp)
Multiple Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/multiple_threads.asp)
Multithreading for Rookies (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndllpro/html/msdn_threads.asp)
FAQs:
How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?t=312452)
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
How to use member functions as thread functions? (http://www.codeguru.com/forum/showthread.php?t=312453)
How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)
canus
June 30th, 2005, 02:04 PM
Hey thanks for the info, I think I got it working.
First of all, are you trying to call MapWindowPoints from inside your secondary thread?
Next, when working with MFC there are about four types of functions (or methods):
1) Non static class methods
2) Static class methods
3) MFC global functions
4) Windows api's
For non static class methods you need to call them via their instance variable:
pWnd->MapWindowPoints(...);
For static methods call them via the class scoping operator:
CMyClass::StaticMethod(...);
The MFC global function are usually prefixed by Afx and are called as follows:
CMainFrame* pMainFrame = AfxGetMainWnd();
Windows api's are called directly. To avoid ambiguity between MFC class methods, use the scoping operator. NOTE: Many api's take an additional parameter than the MFC counterparts because they require a handle of some sort. The MFC api equivalents typically store this handle in the class instance and as such don't require the param being passed to the method.
::MapWindowPoints(hWnd, ...); //NOTE: the :: scoping and extra param
Arjay
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.