Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multithreaded Problem

Status
Not open for further replies.

TheCrappyProgrammer

Programmer
Sep 17, 2001
41
US
In my

static UINT CProgram::DoIt(LPVOID param);

I am not allowed to call

UpdateData(FALSE);

from the MFC library. The compiler says that I can't call it because I am calling it from a static function. Yet the Multithreaded Tutorials on the internet states that I have to write the prototype this way. How do I call UpdateData?
 
I think you have to give a little more information about what classes you are dealing with :

Is UpdateData() a non-static member of CProgram or is UpdateData() the one in class CWnd or ....

If member of CWnd, I would suggest you do like this :
pWndObject -> UpdataData(FALSE);

where pWndObject is a pointer to the object you want to operate on.

/JOlesen

/JOlesen
 
Yes, UpdateData() is a CWnd. This is exactly how that portion looks like:

UINT CProgram::DoIt(LPVOID param)
{
CProgram* ptr = (CProgram*)param;
ptr->UpdateData(FALSE); // Errors Out Here

...
...
}

DoIt(LPVOID param) is a static private member function of CProgram. I invoked the function like this:

BOOL CProgram::OnInitDialog()
{
AfxBeginThread(&DoIt,(LPVOID)this,THREAD_PRIORITY_NORMAL);
...
}

So what's the problem?

 
So what's the problem? .... I simply don't know !

I just reviewed some old multithreaded code I wrote years ago, and I use exactly the same approach - only it wasn't MFC.
Could it be an MFC issue ?

After searching on Google, I found some warnings on using UpdateData() ... is this what you want to ?

Could you post the precise compiler errors ?
/JOlesen
 
Well if I just use UpdateData(FALSE), then I get a compiler error:

error C2352: 'CWnd::UpdateData' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2230) : see declaration of 'UpdateData'

However, if I use ptr->UpdateData(FALSE), then I get a runtime error:

Debug Assertion Failed!
File: wincore.cpp
Line: 884
 
Oh, then it's not a compiler Error / problem.

You SHOULD use the latter method (ptr -> UpdateData()) !

There is something wrong with the way you use the class.
The Assert is put there (in WinCore.cpp) to protect against bad usage.
I am no expert on MFC, but I would try to debug your code right till the Assertion occurs - and see what's being tested and what fails.
MFC source code can be / is installed, so you should be able to debug all the way.
/JOlesen
 
Just looked into wincore.cpp and found this right after the assert :

CObject* p;
ASSERT((p = pMap->LookupPermanent(m_hWnd)) != NULL ||
(p = pMap->LookupTemporary(m_hWnd)) != NULL);
ASSERT((CWnd*)p == this); // must be us

// Note: if either of the above asserts fire and you are
// writing a multithreaded application, it is likely that
// you have passed a C++ object from one thread to another
// and have used that object in a way that was not intended.
// (only simple inline wrapper functions should be used)
//
// In general, CWnd objects should be passed by HWND from
// one thread to another. The receiving thread can wrap
// the HWND with a CWnd object by using CWnd::FromHandle.
//

// It is dangerous to pass C++ objects from one thread to
// another, unless the objects are designed to be used in
// such a manner.

This could give you an idea how to handle the problem.
/JOlesen
 

You can not call a static or private function from a thread. Even if the thead is defined in the class. Create another function called MyUpDate() as part of CProgram class. In DoIt() call CProgram->MyUpDate(). In MyUpDate() call UpdateData().

Ex:

BOOL CProgram::OnInitDialog()
{
AfxBeginThread(&DoIt,(LPVOID)this,THREAD_PRIORITY_NORMAL);
}

UINT CProgram::DoIt(LPVOID param)
{
CProgram* ptr = (CProgram*)param;
ptr->MyUpDate(FALSE);
}

void MyUpDate(BOOL bUpDateType){
UpdateData(bUpDateType);
}

Problem solved.

Brother C

PS make MyUpDate() public or it will not work.
 
Dear BrotherC,

Indeed you can call a memberfunction from a thread belonging to the same class - no matter if the function being called is public, protected or private.
The only thing is that the function being called should be written in a way so it can handle this.

I am pretty sure TheCrappyProgrammer's problem is MFC - or rather the way MFC is being used.




/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top