Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to execute a dialogue class function from a thread?

For example
C++
		static DWORD WINAPI setBitThread(LPVOID lpParam) 
{	
	CUMKurunegalaDlg::OnCbnEditchangePtype(); /*This line give me an error that tell "Non-static member reference must be relative to a specific object */
    return 0; 
} 
Posted
Comments
pasztorpisti 18-Aug-12 4:42am    
Threading is an advanced topic (especially when mixed with gui). For this reason I wouldn't jump on it before having a solid understanding of the language constructs...

I answered a question like this not too long ago on a message board. Someone else wanted to pop up a messagebox from a thread. Here is my answer to that:
http://www.codeproject.com/Messages/4333885/Re-Messagebox.aspx[^]
In your case you should execute your dialog class function (generally your gui manipulator code) in the WM_USER+XXX message handler of your window, where the other guy show the messagebox.

Here is another short answer to something else, very important if you are using MFC:
http://www.codeproject.com/Messages/4334909/Re-Messagebox.aspx[^]

EDIT: Actually you have two problems: you don't know how to trigger that action in your dialog, another is that you are trying to do that from a non-gui thread. The first problem is already described by nv3 in another solution.
 
Share this answer
 
v2
Comments
nv3 18-Aug-12 5:32am    
Likewise! You brought in the other half.
Sergey Alexandrovich Kryukov 18-Aug-12 18:11pm    
5ed, with the references post as well.
--SA
pasztorpisti 18-Aug-12 18:24pm    
Thanks!
Your question is unclear to me. But it seems that you are trying to call a non-static (i.e. normal) member function from a static member function. To call a non-static member function you need an object of this class to apply the function to. For example, when you call

pObj->MyFunction ();


then MyFunction is applied to the object that pObj points to. Inside MyFunction the this pointer then points to that very object.

The nice thing about a this pointer is that you address other member variables or member functions of a class without using a pointer. For example inside MyFunction to following two statements do the same:

this->m_counter = 0;
m_counter = 0; // does the same


or another example:

this->OtherFunctionOfThatClass();
OtherFunctionOfThatClass(); // does the same


Now, static member functions are different in that they don't need and don't have this pointer. That means you can call them from outside your class although you don't have an object to apply the to.

Inside a static member function you cannot call a non-static member function of your class without explicitly telling the object you want to apply the function to, because your non-static member function has not this pointer.

If you want to call a non-static member from a static member function you must supply an object as a pointer or a reference:

void MyClass::MyStaticMemberFunction()
{
    MyNonStaticMemberFunction(); // this is an error
    ...
    MyClass* pOtherObj = ...
    pOtherObj->MyNonStaticMemberFunction(); // that will work
}
 
Share this answer
 
Comments
pasztorpisti 18-Aug-12 4:39am    
5ed, You answered the actual problem of OP, for some reason I saw only the other problem... :-)
Sergey Alexandrovich Kryukov 18-Aug-12 18:11pm    
5 from me, too.
--SA
nv3 18-Aug-12 18:43pm    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900