Does using AfxBeginThread() require AfxEndThread()?


    Sponsored Links


  • 1. The symbol 'OnTest' is undefined
    "Steve Russell" < XXXX@XXXXX.COM > wrote in message news:% XXXX@XXXXX.COM ... >I just added a handler to my view. When I right click on it and choose 'Go >to definition' or 'Go to reference', I get the message, 'The symbol >'OnTest' is undefined." Yet everything seems to be working fine. What >would cause this or fix this? > I notice that every function I am now adding has this same situation. Having made quite a few changes in the program lately, I am wondering, Is there a setting that I might have changed that would be the culprit?
  • 2. MFC71 Merge Modules
    I'm using the Windows Installer included in VC++ .NET 2003 to create an installation setup for my MFC project. I have added merge modules for MFC7.1 which means that the MFC Dll's are installed in my applications folder. It's just that I have a sub-folder called bin where I want to put all my executables and dll's. How can I change the destination for the files in the merge modules so they are put in my \bin sub-folder instead of directly under my applications folder? I tried to hack the .msm files using Orca but I wasn't able to figure out if it was possible to change the behavior this way.
  • 3. OnCreate of CFrameWnd based splitter pane gets called twice!!!
    Hi All, I have a splitter window, in which I want to have 3 panes (as vertical columns). My primary requirement is I want a menu inside one of the panes, i.e, menu should belong to the pane and not the whole window. To achieve this, I put a CFrameWnd derived class inside the pane. This CFrameWnd derived class has the menu I want. Now, my problem is that after I do this, the OnCreate of the FrameWnd is getting called twice all the time. Can anyone help me know why this is happening? Or even better, does anyone have an alternate solution to putting a menu in each pane of a splitter window? Thanks R
  • 4. VC++ .NET 2003
    Hello, Does VC++ .NET 2003 have a class Wizard or a similar tool? I heard that VC++ 2003 .NET has something called "handlers" what are they? In short, I know nothing about VC++ .NET 2003, is there a website where I can learn about its functionalities? And, what would be the best IDE to write an FTP application for the internet: VC++ 6 or VC++ .NET 2003? Your response will be greatly appreciated. Thank you, Victor.
  • 5. Need ShowWindow twice
    I create a modeless dialog that starts off invisible (ie, the WS_VISIBLE flag isn't set). At some point, the user makes a selection and I call: myModelessDlg->ShowWindow( SW_SHOWNORMAL ); on it. But I've run into a situation where it seems like the first time I want it to show up, I need to call ShowWindow twice. Both calls return 0 saying it was not currently visible, but the second one always makes it show up. Any ideas on why this might be true? A co-worker said he had noticed in the past that often, the first ShowWindow call would just do whatever the default creation was, no matter what. So now I call ShowWindow with SW_HIDE immediately after creating the dialog: CMyModelessDlg* myModelessDlg = new CMyModelessDlg(); myModelessDlg->ShowWindow( SW_HIDE ); and now the first call to ShowWindow with the SW_SHOWNORMAL works as expected. Any other ideas? -- Jonathan Arnold inSORS

Re: Does using AfxBeginThread() require AfxEndThread()?

Postby Scott McPhillips [MVP] » Thu, 02 Sep 2004 08:11:06 GMT

The call to AfxEndThread is not needed and is considered harmfull.  The 
reason it is not needed is that the return 0 returns to MFC code that 
closes the thread handle.  The reason it is considered harmfull is that 
it never returns.  The thread ceases to exist during the call, and 
therefore the destructors of any objects created on the stack never get 

Scott McPhillips [VC++ MVP]

Re: Does using AfxBeginThread() require AfxEndThread()?

Postby Joseph M. Newcomer » Thu, 02 Sep 2004 23:33:06 GMT

No, and I consider using AfxEndThread to be poor programming practice. A thread should
terminate solely by returning from its top-level function. The place where you showed
AfxEndThread is pointless, because if you look at the code in MFC, you see that it will be
called almost immediately upon return anyway, but I believe that the return should never
be short-circuited.

Joseph M. Newcomer [MVP]
Web:  http://www.**--****.com/ 
MVP Tips:  http://www.**--****.com/ 

Re: Does using AfxBeginThread() require AfxEndThread()?

Postby Joseph M. Newcomer » Thu, 02 Sep 2004 23:34:29 GMT

You missed the part where I set m_bAutoDelete to FALSE before doing the ResumeThread.

Joseph M. Newcomer [MVP]
Web:  http://www.**--****.com/ 
MVP Tips:  http://www.**--****.com/ 

Re: Does using AfxBeginThread() require AfxEndThread()?

Postby Joseph M. Newcomer » Thu, 02 Sep 2004 23:35:50 GMT

ust make sure that the m_bAutoDelete is set before the ResumeThread.

On Tue, 31 Aug 2004 15:09:04 -0700, RNEELY < XXXX@XXXXX.COM > wrote:

Joseph M. Newcomer [MVP]
MVP Tips:

Similar Threads:

1.Using AfxBeginThread to create a worker thread...

Code that creates the thread...

m_pListenerThread = (CWinThread*)AfxBeginThread(
(AFX_THREADPROC)ListeningThreadFunction, (LPVOID)this );
ASSERT( m_pListenerThread );

Function that the thread is to run (code stripped out)...

UINT ListeningThreadFunction( LPVOID in_pParam )

In both Debug and Release builds, the call to AfxBeginThread hangs
infinitely in thrdcore.cpp, specifically at the function:

BOOL CWinThread::CreateThread(DWORD dwCreateFlags, UINT nStackSize,

at the WaitForSingleObject line:

VERIFY(ResumeThread() != (DWORD)-1);
VERIFY(::WaitForSingleObject(startup.hEvent, INFINITE) == WAIT_OBJECT_0);

Now, it has been a long time since I've used a worker thread as opposed to a
GUI thread and I remember that they were trivial to use...

Any suggestions why AfxBeginThread would hang itself?

BTW, the overview of how this all gets called is:

    Application calls an exposed DLL function, the DLL instantiates and
creates a series of classes to use internally, eventually the class which
needs this worker thread to 'listen' gets created.  After the class is
created, an initialization function is called on the class which creates
this thread as you see above.

    Any glaringly stupid things I'm doing/forgetting?


2.Using Afxbeginthread

I have used AfxbeginThread with ThreadProc as first argument. i wantd to ask
what is the use of second type of AfxBeginThread that takes CRunTimeClass *
as parameter..also how to use it..i mean in the first type we just have to
write our code to be executed in the thread with in that Thread function
that is passes as first argument to AfxBeginthread, but what about the other
type..where do we write our code in the second type of this API. could
someone please give me an example coz i cud'nt understand whats written in
msdn..also i didnt find any good example clearly indicating API usage with
CRunTime argument.

Thanx and Regards

Usman Jamil

3.Not understanding 'AfxEndThread'

Hello all,

   I have an app that creates a UI thread, and a worker thread to wait for 
the handle of the UI thread.   But, in the UI thread when I do a 
I get an Assert error:

In the app's view:
void CJunkThreadView::OnButton1() 
    pAnyThread = (CAnyThread*)AfxBeginThread(RUNTIME_CLASS(CAnyThread),

     //fire off a worker thread to wait for the UI's handle
     if(!(pWrkThd_1 = AfxBeginThread(WrkThread_1,this)))

    //Let the UI go
void CJunkThreadView::RunUIThread()
  pAnyThread->m_bAutoDelete = FALSE;

UINT WrkThread_1(LPVOID lParam)
    CJunkThreadView* pMe = (CJunkThreadView*)lParam;

          case WAIT_OBJECT_0:
          case WAIT_TIMEOUT:
   return 1;

 'CAnyThread' is the UI thread whose base class is CWinThread and for testing
it has only one function:
void CAnyThread::RunIt()
    AfxMessageBox("Ending Any Thread");

   /*causes assert */

   I've tried just returning from 'RunIt', which doesn't cause an assert, 
but I
don't get a ' pMe->pAnyThread->m_hThread' handle signalled in the waiting
worker thread.

   Obviously, I don't understand something? Any help appreciated.


Ray K.


4.Is there any difference between AfxEndThread and TerminateThread ?


Is there any difference between AfxEndThread and TerminateThread ? I
know we can use AfxEndThread from within the thread only but any other
difference like, cleaning up or so.. ??


5.thread never calls AfxEndThread


I have made a simple app with only one button that starts or stop a loop.
This loop is actually a thread.
In my OnInitDialog I create the thread in a suspended state and when I 
click on Start I resume the thread and when it's stop I suspend it.
Here is the code

CTestDlg* pThis;

BOOL CTestDlg::OnInitDialog()
if (!(m_pThread = AfxBeginThread(
		AfxMessageBox(_T("Cannot create thread"));
		return false;

/* static */
UINT CTestDlg::thScanCarte( LPVOID pParam )
	  pThis = (CTestDlg*) pParam;
	  return pThis->thScanCarte();

UINT CTestDlg::thScanCarte()
	USHORT status;
	CString s;
	bool bFlag = true;

		if (m_bExitThread){
			TRACE(_T("exit thread"));
	return 0;

void CTestDlg::OnClose()
	USHORT status;

	m_bExitThread = TRUE;


The problem is when my app exitI cannot see the trace 'exit thread'.
I think this code never called but why ?
When the app exits I suspend the thread so I suppose this is why
it is never called.

6. AfxEndThread problem

7. Can this be done using C#

8. Can this be done using a Data adapter??

Return to VC


Who is online

Users browsing this forum: No registered users and 4 guest