Does using AfxBeginThread() require AfxEndThread()?


    Sponsored Links


  • 1. Help with CStringArray in Unicode, Multi-byte, & MFC
    Hi, I couldn't find the "development" group so i chose this one. I am trying to bring some code that has the CStringArray in it from a Multi-Byte project into a Unicode project and I keep getting errors I cannot resolve. Scenario: Project type: DLL Use MCF: Shared DLL Use ATL: Not using ATL Min CRT Use in ATL: No Char set: Unicode added afxcoll.h to stdafx.h as suggested elsewhere #include <afxcoll.h> #include "resource.h" #include <atlbase.h> #include <atlcom.h> I get the following error; Error 247 error LNK2005: _DllMain@12 already defined in TestProj.obj mfcs80u.lib How do I fix this? I have been looking for days and everything I try, from creating a wrapper, to adding header files, changing character sets, ATL, MFC, and all kinds of other hoops. I'm about ready to scream real loud - which would probably get me in trouble. Can anyone please help me? Thanks in advance for your assistance. It is greatl appreciated. Regards, Gary A
  • 2. Text box using rich edit control
    Hi, I have implemented a text box using CRichEditCtrl child window in a CView based application. I am facing a problem with zoom in and zoom out functionality with respect to text box. I am setting the current zoom level of CRichEditCtrl same as that of CView. I have wrap text on for the text box and the text in the text box repositions itself in different ways at different zoom levels. For example, if I have a text box of width 300 pixels wide, and I have wrapped text in 5 lines at 100% zoom, same text is now shown in the same way at 50% zoom. It gets wrapped at different places and is getting shown in 4 lines. But, what I need is that text should remain static across all zoom levels, it should not alter its position. If anyone has faced similar situation and able to solve it, then please let me know how you are able to make it work. In MS Word, text box that is inserted using Insert -> Text Box command behaves perfectly fine. How to get such functionality? To summarize, how can I get WYSIWYG text box using CRichEditCtrl across all zoom levels? thanks for your help
  • 3. Inline class members
    "Jeova Almeida" < XXXX@XXXXX.COM > ha scritto nel messaggio news:% XXXX@XXXXX.COM ... > I'm creating some new C++ classes (VS2008) and I'm inclined to create > their methods as inline functions. > > My questions are: > > a) Is there a real advantage to create the method body in a separate .cpp > file (that is, not inline)? I think that the main advantage is about readability, i.e. you can read the header and have an idea of the class interface, and you are not distracted by method implementations (that you can study in the .cpp file). > b) When I #include a file on other files which use them, the compiler will > include the inline function, say a long one, every time I include the > class? How does it work internally? I think that "inline" keyword is just an hint to the compiler, a suggestion, a request, but *not* an "order". I mean: the compiler has the final word about making the function inline or not, and I believe that the compiler will not inline big functions (that would be a disaster causing code bloating). Giovanni
  • 4. How to Locate a registry entry created by Windows Installer
    Hello, I have created an application that is installed using Windows Installer 4.5 As th installation runs, the User is prompted for a Serail Number. The Serial Number is saved by WI in the registry. When I use RegEdit, I can search the registry and I can find the value. It is in HKLM\Software\Microsoft\Windows\Installer\UserData\Products\something that looks like a GUID\InstallProperties\ProductID My question really is, how can I locate the GUID so that I can read the ProducID value from the proper Registry entry? I have used Orca to open the MSI, but I can not see the GUID anywhere within the package. Maybe it is not even a GUID? D208A67257CDC374FB87748C044845B75 I suppose I could search for my application name. I am just not sure if I can even do this. I just want to display the Serial Number in the About dialog box for the application. Just a nice to have really. Thanks

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 54 guest