Click to See Complete Forum and Search --> : Thread: Object instance(Call its functions & attributes) in a thread?


myy
February 14th, 2006, 05:08 PM
Hello,

I have been working on this issue for few days with no luck .
I am sorry for my simple questions as I have never done any work in VC++ before.

I have to modify a VC++ open source software[MyPhone]. What I wanted to do is:

I have a class [CMyPhoneDlg], the structure of this class is:

class CMyPhoneDlg : public CDialog {
public:
CMyPhoneDlg(CWnd* pParent = NULL);
~CMyPhoneDlg();
...
PString m_token;
CMyPhoneEndPoint m_endpoint;

protected:

afx_msg void OnCall();
...
}

In the function [OnCall()], I need to start a thread:
OnCall() {
..
AfxBeginThread(ServerThread,this);
..
}

UINT ServerThread(LPVOID pParam) {
... I need to call, might be
m_endpoint.MakeCall((const char *)curDest, m_token);
.. Or call
CMyPhoneDlg.MyOwnFunction();
}

There might be a few ways to do this such as use of global variable, pass the object instance to the ServerThread, Singleton, etc.

Because I have no experience in VC++, so this work is hard to me. I tried gloabl variable, and got this:
error C2228: left of '.OwnMakeCall2' must have class/struct/union type

I tried to create a static method in CMyPhoneDlg:

void CMyPhoneDlg::OwnMakeCall2()
{
m_endpoint.MakeCall((const char *)"192.168.1.5", m_token);
}

and got errors if i try to call m_endpoint.MakeCall() :
* error C2228: left of '.MakeCall' must have class/struct/union type
* illegal reference to data member 'CMyPhoneDlg::m_token' in a static member function.

This work made me really headache. May anyone help me?

Thanks a lot.
myy

Andreas Masur
February 14th, 2006, 05:39 PM
Since you are passing the 'this' pointer of the dialog class where the 'CMyPhoneEndPoint' class is a member, you would need to do

UINT ServerThread(LPVOID pParam)
{
CMyPhoneDlg* parent = static_cast<CMyPhoneDlg*>(pParam);
parent->m_endpoint.MakeCall((const char *)curDest, m_token);
}

myy
February 14th, 2006, 07:07 PM
Big thanks, Andreas.

You are great, It works.

myy

kirants
February 14th, 2006, 07:53 PM
While it is the way to use the param passed, note that, herein, the param being a MFC object, it is unsafe to do it.

MFC objects aren't safe to be passed across threads and you may see unexpected behavior if you try to do.. Just a word of caution.
See this FAQ:
MFC Thread: How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

myy
February 14th, 2006, 11:06 PM
While it is the way to use the param passed, note that, herein, the param being a MFC object, it is unsafe to do it.

MFC objects aren't safe to be passed across threads and you may see unexpected behavior if you try to do.. Just a word of caution.
See this FAQ:
MFC Thread: How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

Oh, ya. You are right, kirants.
I will have a look at that, Thanks.

Actually I hope my work is not that hard. I am just given a task to do a socket stuff for VC++ <---> Flash. And there is only one client(flash) and one server(vc++). That means i need to use of flash to call the functions in VC++ to do some work(no actions will be taken from VC++ UI or whatever). I hope it is still safe.

What I want is just "It works, and will not crash" at the moment.

:)

Marc G
February 15th, 2006, 05:36 AM
[ moved thread ]