Solved

AfxBeginThread

Posted on 1997-01-05
1
2,595 Views
Last Modified: 2013-11-20
JRUDD has helped to to realize that the prototype for a worker thread member function should be declared like this:

private:
static UINT TestThread( LPVOID );

and called like this:

 AfxBeginThread( TestThread, NULL );

I can not figure out what parameter I place in the body of TestThread. For example:

UINT  Nimzo::TestThread(  ???  )
{
      return 0;
}

What should I use to replace the question marks? Nothing I have tried has worked.

I'm using VC++ 4.0 Standard Edition.

Once again, your help is appreciated.
0
Comment
Question by:ldorfman
1 Comment
 

Accepted Solution

by:
jrudd010197 earned 100 total points
ID: 1300093
Yes you can use a member function.  The way I show you is the most common way.

Make your member function static.  The reason for this is simple.  Every C++ member funcation has a hidden parameter - the "this" pointer.  Even though you don't pass it in it is there.  So here is a simple example of how to do it.

//header file
class CMyDialog : public CDialog
{
//regular CMyDialog stuff here

private:
//make it private so no one but this class can //use it
static UINT MyThreadProc( LPVOID );
};

void CMyDialog::OnMyFakeButtonClick()
{
     AfxBeginThread( MyThreadProc, NULL );
}

Now of course with the function being static that means you can't touch anything that is in your class that isn't static as well.  So to beat this you pass in the "this" pointer as your LPVOID parameter in AfxBeginThread.  

Of coures you can also use the other AfxBeginThread.  The one that takes a Runtime class pointer.  Then if you are using VC++ 4.2 inside your thread class you can use the macro ON_THREAD_MESSAGE.  This ( in my humble opinion ) is the cleanest and nicest way to do it.  Because to make the thread quit all you have to do is do this...

m_pThread->PostThreadMessage( WM_QUIT, 0, 0 );

Hope this helps
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question