Does using AfxBeginThread() require AfxEndThread()?

VC

    Sponsored Links

    Next

  • 1. How to disallow user to select CListCtrl
    Hello, I am curious on how I could not allow the user to select an entry from the list control box. So the box will act and function normally, except when the user selects on the listctrl box, it will not hilight. how would I go about doing this? Thanks ahead of time..
  • 2. GetPrivateProfileSectionName won't work in VC++.NET 2002
    How do I get GetPrivateProfileSectionName function to work in VC++.NET 2002? It works fine in the 2003 version, but I can't get it to work in the 2002 version which is what I use at home. Z.K.
  • 3. RegQueryValueEx and UNICODE.
    Hello everybody. I Have a function to extract char data from the registry, but I don't know if it is UNICODE compliant, this is my code: BOOL CRegistry::Read (LPCTSTR pszKey, CString& sVal) { ASSERT(pszKey); DWORD dwType; DWORD dwSize; LONG lReturn; //m_hKey already is ok: lReturn = ::RegQueryValueEx (m_hKey, pszKey, NULL, NULL, NULL, &dwSize); if (lReturn != ERROR_SUCCESS) return FALSE; //Is this UNICODE compliant? char *pszChain = (char*)malloc(dwSize + 1); if (!pszChain) return FALSE; dwSize++; lReturn = ::RegQueryValueEx (m_hKey, pszKey, NULL, &dwType, (LPBYTE)pszChain, &dwSize); if (lReturn != ERROR_SUCCESS) { free(pszChain); return FALSE; } sVal = pszChain; free(pszChain); return TRUE; } Thanks in advance, William GS
  • 4. CCriticalSection - does my thread already have a lock?
    I have a thread pool waiting on an IOCP. Several threads can wake-up at the same time, and have received data from the exact same socket. I have the data associated with the socket locked via a CCriticalSection. But, during shutdown, I want to manipulate the data -- if it isn't locked down. I am sharing a routine between these two thread-types (worker and main). This routine can delete the object, IF the current thread has the lock -- if the current thread does NOT have the lock, then it needs to flag the object for later deletion. So, the question: Can I query a CCriticalSection object to determine if the current thread has a lock? Thanks DanB
  • 5. Does MSHTML handle script?
    When a web page contains script codes like JavaScript or VBScript, IE can view the page correctly as long as you don't make configurations to disallow it. I am wondering where does IE handles these scripts. Something out of IE like WSH, or something inside IE? Can I do that in MSHTML? If a page contains a piece of script that draws an image at a certain location, can MSHTML understand that? I mean, in the HTML DOM tree, there is only a script element; or, there are two elements - the script element and the image element? Thank you.

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 
invoked.

-- 
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.
					joe





Joseph M. Newcomer [MVP]
email:  XXXX@XXXXX.COM 
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.
				joe






Joseph M. Newcomer [MVP]
email:  XXXX@XXXXX.COM 
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.
joe

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


Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

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 )
{
    while(1)
    {}
}

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,
LPSECURITY_ATTRIBUTES lpSecurityAttrs)

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?

    WTH


2.Using Afxbeginthread

Hi
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 
'AfxEndThread(0,0)'
I get an Assert error:

In the app's view:
void CJunkThreadView::OnButton1() 
{
    pAnyThread = (CAnyThread*)AfxBeginThread(RUNTIME_CLASS(CAnyThread),
	         THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
    ASSERT_VALID(pAnyThread);

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

    //Let the UI go
     RunUIThread();
}
void CJunkThreadView::RunUIThread()
{
  pAnyThread->m_bAutoDelete = FALSE;
  pAnyThread->ResumeThread();
  Sleep(1000);
  pAnyThread->RunIt();

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

    switch(WaitForSingleObject(pMe->pAnyThread->m_hThread,10000))
       {
          case WAIT_OBJECT_0:
		 AfxMessageBox("0");
		 break;
          case WAIT_TIMEOUT:
		 AfxMessageBox("1");
		 break;
       }
   return 1;
}

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

   /*causes assert */
   AfxEndThread(0,0);
}


   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.

TIA,

Ray K.

 

4.Is there any difference between AfxEndThread and TerminateThread ?

Hi,

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.. ??

Jigar

5.thread never calls AfxEndThread

Hi,

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(
		thScanCarte,
		this,
		THREAD_PRIORITY_NORMAL,
		0,
		CREATE_SUSPENDED,
		NULL)))
	{
		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;

	while(1)
	{
		if (m_bExitThread){
			TRACE(_T("exit thread"));
			AfxEndThread(1);
		}
		
		TRACE(_T("scanthread\n"));
		Sleep(800);
	}
	return 0;
}


void CTestDlg::OnClose()
{
	USHORT status;

	StopThread();
	m_bExitThread = TRUE;
	m_card.Close();

	CDialog::OnClose();
}


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