Solved

AfxBeginThread

Posted on 1997-01-05
1
2,579 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now