calling a class member function in a seperate thread
-
hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan
-
hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan
Pass the address of the instance of the class from which you want to call a member function as an argument of your thread. When you start a thread, you can pass a user defined parameter that will be passed to the function of your thread. There, pass the pointer to your class instance. In your thread function, cast it back to the class pointer and call the member function.
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
hello everybody !! my new problem is that i have a class that contain a some functions and some variables i want to execute one of these functions in a separate thread using AfxBeginThread(...); and FYI this function uses some member variables of the class to do it's work any idea ??? thnx 4 ur time and concern a.hemdan
Hi I have a friend who somehow encountered that and i remembered that he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread. Try to research about worker thread and UI thread, you may find how to use member viriables of a class inside the thread by using struct. Hope i have helped you somehow. :)
i need to learn more... i want to be like you guys... i'm just a begginer
-
Hi I have a friend who somehow encountered that and i remembered that he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread. Try to research about worker thread and UI thread, you may find how to use member viriables of a class inside the thread by using struct. Hope i have helped you somehow. :)
i need to learn more... i want to be like you guys... i'm just a begginer
rupert_durans wrote:
he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread
:confused: What do you mean by making a structure of the class ? Why not simply pass the address of the class instance (the this pointer) to the thread ?
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
rupert_durans wrote:
he made a structure of the class and pass it on to the thread so that the member viriable can be used by the thread
:confused: What do you mean by making a structure of the class ? Why not simply pass the address of the class instance (the this pointer) to the thread ?
Cédric Moonen Software developer
Charting control [Updated - v1.1]yup, i asked him again and you are right, you may or may not create struct as long as it points to the class instance.
i need to learn more... i want to be like you guys... i'm just a begginer
-
yup, i asked him again and you are right, you may or may not create struct as long as it points to the class instance.
i need to learn more... i want to be like you guys... i'm just a begginer
Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?
Cédric Moonen Software developer
Charting control [Updated - v1.1]hehehe, soweee, i'm just a newbie, i just saw it on my friend's code... :) now i know that it can be direct, can you please give me example on how to declare a pointer to the class instance?
i need to learn more... i want to be like you guys... i'm just a begginer
-
Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?
Cédric Moonen Software developer
Charting control [Updated - v1.1]is it like this one for example? CMyFileSpyDlg* _this;
i need to learn more... i want to be like you guys... i'm just a begginer
-
Ok but why do you want to do such a thing ? What is the advantage of wrapping your class into a structure just to pass it to your thread ? Pass it directly, it's much cleaner. Why adding complexity where this is not required ?
Cédric Moonen Software developer
Charting control [Updated - v1.1]can you also answer my question above? HOw to pause a spawned process?
i need to learn more... i want to be like you guys... i'm just a begginer
-
hehehe, soweee, i'm just a newbie, i just saw it on my friend's code... :) now i know that it can be direct, can you please give me example on how to declare a pointer to the class instance?
i need to learn more... i want to be like you guys... i'm just a begginer
rupert_durans wrote:
can you please give me example on how to declare a pointer to the class instance
You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:
CMyClass Inst;
Here,
Inst
is an instance of CMyClass. The address of this class is obtained with the & operator:CMyClass* pPointer = &Inst;
If you pass this address as a parameter to your thread function, you'll be able to access its member:
void MyThreadFunc(LPVOID pParam)
{
CMyClass* pClass = (CMyClass*)pParam;
pClass->MemberFun();
}You can also start your thread inside a member function of your class, then simply pass the this parameter:
void CMyClass:SomeFunction()
{
_beginthread(MyThreadFun,0,this);
}
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
rupert_durans wrote:
can you please give me example on how to declare a pointer to the class instance
You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:
CMyClass Inst;
Here,
Inst
is an instance of CMyClass. The address of this class is obtained with the & operator:CMyClass* pPointer = &Inst;
If you pass this address as a parameter to your thread function, you'll be able to access its member:
void MyThreadFunc(LPVOID pParam)
{
CMyClass* pClass = (CMyClass*)pParam;
pClass->MemberFun();
}You can also start your thread inside a member function of your class, then simply pass the this parameter:
void CMyClass:SomeFunction()
{
_beginthread(MyThreadFun,0,this);
}
Cédric Moonen Software developer
Charting control [Updated - v1.1]thank you :cool:
i need to learn more... i want to be like you guys... i'm just a begginer
-
rupert_durans wrote:
can you please give me example on how to declare a pointer to the class instance
You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class:
CMyClass Inst;
Here,
Inst
is an instance of CMyClass. The address of this class is obtained with the & operator:CMyClass* pPointer = &Inst;
If you pass this address as a parameter to your thread function, you'll be able to access its member:
void MyThreadFunc(LPVOID pParam)
{
CMyClass* pClass = (CMyClass*)pParam;
pClass->MemberFun();
}You can also start your thread inside a member function of your class, then simply pass the this parameter:
void CMyClass:SomeFunction()
{
_beginthread(MyThreadFun,0,this);
}
Cédric Moonen Software developer
Charting control [Updated - v1.1]Cedric Moonen wrote:
You don't really 'create a pointer to the class instance'. What you have in fact is an instance of your class: CMyClass Inst; Here, Inst is an instance of CMyClass. The address of this class is obtained with the & operator: CMyClass* pPointer = &Inst;
Hi Cédric, Isn't "Inst" an instance of the class and "pPointer" a pointer to an instance of the class? ;) Mark