Click here to Skip to main content
12,755,095 members (36,908 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Visual-Studio MFC
How to execute a dialogue class function from a thread?

For example
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 17-Aug-12 20:08pm
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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:[^]
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:[^]

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.
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.
pasztorpisti 18-Aug-12 18:24pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:

    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
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.
nv3 18-Aug-12 18:43pm

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 18 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100