AfxBeginThread() with a function in the main class
-
This is allways something wich i have had trouble with, usualy i end up doing some ugly trick to make it work.. But this time i cant.. (well ive been spending around 3 days trying..) I need a thread which is connected to the main class of my MFC Dialog based application. I looked at Code Project:Using AfxBeginThread...[^] In which i end up createing a new static function that seems to work with some functions.. But when i tried calling the function i wanted it to run, it couldnt since it wasnt a static function.. So i tried making it a static function, but then i got loads of error messages... So how would i go about creating and running a thread that is connected to the main class (like "CThreadDemoDlg::OnStart()" ) ? The function doesnt need any params or other advanced features, maybe i should start it using a differnt function? Or how should i go about this? thanks! PS: Im not really that good at the terminology for c++, or c++ programming itself, so nothing too hightech or advanced unless its a must do..
//Johannes
The thread procedure accepts a void pointer which you can pass when you create the thread. If you pass a pointer to an object of a class then you will have access to that object from the thread. If the thread proc is a static member function of that class, you have complete access (to public/protected/private members).
class CThreadDemoDlg : public CDialog
{
static UINT __cdecl MyThreadProc(LPVOID pParam);
protected:
CWinThread *pAnotherThread;
public:
void SomeThreadStarterFunc();
};void CThreadDemoDlg::SomeThreadStarterFunc()
{
pAnotherThread = AfxBeginThread(&CThreadDemoDlg::MyThreadProc, this);
}UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam)
{
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;...
}Mark
Last modified: 25mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
The thread procedure accepts a void pointer which you can pass when you create the thread. If you pass a pointer to an object of a class then you will have access to that object from the thread. If the thread proc is a static member function of that class, you have complete access (to public/protected/private members).
class CThreadDemoDlg : public CDialog
{
static UINT __cdecl MyThreadProc(LPVOID pParam);
protected:
CWinThread *pAnotherThread;
public:
void SomeThreadStarterFunc();
};void CThreadDemoDlg::SomeThreadStarterFunc()
{
pAnotherThread = AfxBeginThread(&CThreadDemoDlg::MyThreadProc, this);
}UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam)
{
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;...
}Mark
Last modified: 25mins after originally posted --
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line:
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam);
error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)//Johannes
-
Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line:
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam);
error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)//Johannes
Fixed. Sorry about that :) It should be
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Fixed. Sorry about that :) It should be
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;
Mark Salsbery Microsoft MVP - Visual C++ :java:
Ah yes now it compiles, thanks. But when i add a function to it (one that is connected to CThreadDemoDlg) i get the cant call a nonstatic member function.. error C2352: 'CThreadDemoDlg::SomeFunction' : illegal call of non-static member function see declaration of 'SomeFunction' So how should i go about calling the function? thanks again!
//Johannes
-
Ah yes now it compiles, thanks. But when i add a function to it (one that is connected to CThreadDemoDlg) i get the cant call a nonstatic member function.. error C2352: 'CThreadDemoDlg::SomeFunction' : illegal call of non-static member function see declaration of 'SomeFunction' So how should i go about calling the function? thanks again!
//Johannes
Johpoke wrote:
So how should i go about calling the function?
Can you at least bother to show the code that is producing this error? :rolleyes:
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Johpoke wrote:
So how should i go about calling the function?
Can you at least bother to show the code that is producing this error? :rolleyes:
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Doh,
UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam) { CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam; MyFunction(); return 1; }
It looks exactly like that, except the names are different//Johannes
Hint: Ever wonder what
pMyDlg
is being used for?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line:
CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam);
error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)//Johannes
-
Hint: Ever wonder what
pMyDlg
is being used for?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I was thinking i must use it to gain access to the rest of the functions so i like tried :: on it but that didnt work. Now i tried -> on it and all my functions come up :D Thanks alot for the help. The only downside with this is im probably gonna have to go through loads of old programs and get rid of their ugly methods and add this. But i guess its worth it :-D Thanks again :)
//Johannes
-
Johpoke wrote:
but its giving me an error message i really dont understand..
You don't understand a syntax error message but you are working with multi threading *sigh* different day same old garbage
-
-
lol, that dancing dude is funny/weird. My program now works perfectly, thank you all for you help. How does one make that dude anyway? i cant find him...
//Johannes
Johpoke wrote:
My program now works perfectly
If it works, do it.
If it works, do it it works
seems to work probably
^ ^
If it works, do it it worksseems to work probably
^ ^
If it works, do it it works(and might work again)
Cogito ergo sum
(although, admittedly, that is an assumption)
-
:beer:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
lol, that dancing dude is funny/weird. My program now works perfectly, thank you all for you help. How does one make that dude anyway? i cant find him...
//Johannes
Johpoke wrote:
How does one make that dude anyway? i cant find him...
It's right next to the :beer: one. With a little ingenuity, you'll find it :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Johpoke wrote:
How does one make that dude anyway? i cant find him...
It's right next to the :beer: one. With a little ingenuity, you'll find it :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Maybe its like a gold-member only emotion. or maybe it doesnt like me... Ah well doesnt matter im happy anyway :)
//Johannes
Johpoke wrote:
Maybe its like a gold-member only emotion.
Nope. I was so proud when I found it all by myself, since I know nothing about web programming :) ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Johpoke wrote:
Maybe its like a gold-member only emotion.
Nope. I was so proud when I found it all by myself, since I know nothing about web programming :) ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
:laugh:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Ah yes now it compiles, thanks. But when i add a function to it (one that is connected to CThreadDemoDlg) i get the cant call a nonstatic member function.. error C2352: 'CThreadDemoDlg::SomeFunction' : illegal call of non-static member function see declaration of 'SomeFunction' So how should i go about calling the function? thanks again!
//Johannes
u don't understand the problem:(