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?
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
Thanx and Regards
3.Not understanding 'AfxEndThread'
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:
pAnyThread = (CAnyThread*)AfxBeginThread(RUNTIME_CLASS(CAnyThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
//fire off a worker thread to wait for the UI's handle
if(!(pWrkThd_1 = AfxBeginThread(WrkThread_1,this)))
//Let the UI go
pAnyThread->m_bAutoDelete = FALSE;
UINT WrkThread_1(LPVOID lParam)
CJunkThreadView* pMe = (CJunkThreadView*)lParam;
'CAnyThread' is the UI thread whose base class is CWinThread and for testing
it has only one function:
AfxMessageBox("Ending Any Thread");
/*causes assert */
I've tried just returning from 'RunIt', which doesn't cause an assert,
don't get a ' pMe->pAnyThread->m_hThread' handle signalled in the waiting
Obviously, I don't understand something? Any help appreciated.
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
if (!(m_pThread = AfxBeginThread(
AfxMessageBox(_T("Cannot create thread"));
/* static */
UINT CTestDlg::thScanCarte( LPVOID pParam )
pThis = (CTestDlg*) pParam;
bool bFlag = true;
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??