Re: Failed compile when adding AfxBeginThread



Lilith wrote:

On Wed, 07 Sep 2005 15:56:55 -0400, David Wilkinson
<no-reply@xxxxxxxxxxxx> wrote:


Lilith wrote:


I may have brought this up a while back but haven't met with any
success regarding the suggestions I received.  This is frustrating to
the point of needing to post again.

Essentially I'm attempting my first shot at a worker thread.  When the
"go" button is clicked it invokes...

void CUNCTestView::OnUncButton() {
CWinThread *eSafeThread = AfxBeginThread (CUNCTestView::ProcesseSafe, NULL);
}


The header for the thread function is...

UINT CUNCTestView::ProcesseSafe(LPVOID pParam)

The error I get is...

'AfxBeginThread' : none of the 2 overloads can convert parameter 1
from type 'unsigned int (void *)'

I thought that perhaps the problem was the this pointer being passed
to AfxBeginThread so I tried making the function static. That brought
a different complaint from the compiler.


Is it possible that functions passed to AfxBeginThread need to be
non-members of a class?  Should I break off this function from the
class and make it stand alone?

TIA,

Lilith:

The thread function must be a non-class function, or a static member of a class. Show us the error you get when you make the thread function a static member.


It states...

error C2724: 'ProcesseSafe' : 'static' should not be used on member
functions defined at file scope'

This also generates a number of other errors like...

error C2228: left of '.GetWindowTextA' must have class/struct/union
type

This I could understand if making a member function static took the
function out of the class but that's not my understanding of what
static does.

error C2597: illegal reference to data member
'CUNCTestView::m_OutputType' in a static member function

Still don't see why my supposed static function isn't able to address
another class member.  I'm open to education on this.

Amongst the tangle of clases involved in a dialog based program I will
admit that I lose track of what exists within what scope.  I've got a
long way to go before I can envision the components of a dialog in
relationship to the other components.

Thanks,
Lilith

Lilith:

MOst of your immediate problems are really C++ problems, not Windows programming or MFC problems.

1. The keyword static (applied to member function) should be used only in the class definition (.h file) not in the function definition (.cpp file).

2. A static member function cannot access non-static members of the class.

3. You can use the LPVOID parameter of AfxBeginThread() to access the members of your CUNCTestView object (this is what it is for). Like so:

void CUNCTestView::OnUncButton()
{
  CWinThread *eSafeThread =
       AfxBeginThread (CUNCTestView::ProcesseSafe, this);
}

UINT CUNCTestView::ProcesseSafe(LPVOID pParam)
{
  CUNCTTestView* pView = (CUNCTTestView*)pParam;
  // use pView pointer to access non-static members
}

4. See if you can use this information to get your code to compile. But I think you are going to have some other problems at run-time because you seem to be trying to call Windows/MFC functions from your thread function ProcesseSafe(). This is a no-no. Your thread function is not running in the thread that created your windows, and so it cannot manipulate these windows. Rather you should use PostMessage() or SendMessage() to tell the main GUI thread what to do with its windows. The worker thread should only do work.

This seems complicated, but once you have done it once it is easy to do again!

HTH,

David Wilkinson
.