From: Frank on
Dear people,

I create a subthread of my main program by calling


AfxBeginThread(RUNTIME_CLASS(CMySubThread));


CMySubThread is derived from CWinThread. Eventually,
I'd like to terminate the thread. So I clean all variables,
buffer memory and the like, then call AfxEndThread(0)
from within a member function of CMySubThread.

The problem: This call seems to close the whole application,
it throws an exception in:

(code from AfxEndThread):

AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
CWinThread* pThread = pState->m_pCurrentWinThread;
if (pThread != NULL)
{
ASSERT_VALID(pThread);
ASSERT(pThread != AfxGetApp());


The last ASSERT causes the problem.

Now there are two questions:
- Why does the call try to close the whole app
instead of the thread?
- And how can I safely terminate the thread?


TIA!
From: Scott McPhillips [MVP] on
"Frank" <jerk(a)gmx.de> wrote in message
news:ba7f800a-fe53-4bb5-9bee-09865c7b00e8(a)o36g2000vbl.googlegroups.com...
> Dear people,
>
> I create a subthread of my main program by calling
>
>
> AfxBeginThread(RUNTIME_CLASS(CMySubThread));
>
>
> CMySubThread is derived from CWinThread. Eventually,
> I'd like to terminate the thread. So I clean all variables,
> buffer memory and the like, then call AfxEndThread(0)
> from within a member function of CMySubThread.

The thread must terminate itself. Just because you call from within a
member function of the thread class does not mean you are calling from the
thread. I.e., calling a function from the main thread means the function
executes in the main thread, no matter what class the function is part of.

The thread must call PostQuitMessage(0) to exit. Your main thread can
request the secondary thread to exit by posting a custom message to it or by
signaling an event that the thread monitors.

--
Scott McPhillips [VC++ MVP]

From: Frank on
Scott McPhillips wrote:

> The thread must terminate itself. Just because you call from within a
> member function of the thread class does not mean you are calling from the
> thread. I.e., calling a function from the main thread means the function
> executes in the main thread, no matter what class the function is part of.
>
> The thread must call PostQuitMessage(0) to exit. Your main thread can
> request the secondary thread to exit by posting a custom message to it or by
> signaling an event that the thread monitors.

I tried that: Had the main thread post a message
to the thread (PostThreadMessage), in the message handler

- call AfxEndThread
- PostQuitMessage(0)
- m_pMainWnd->PostMessage(WM_CLOSE, 0, 0)
- m_pMainWnd->PostMesage(WM_QUIT, 0, 0)

Everything closes the main program.

Do I have to do something about the message queue
of the subthread or something?
From: Seetharam on
Here is a good example:

http://www.ucancode.net/Visual_C_MFC_Example/WaitForSingleObject-_beginthreadex-SetEvent-Start-Stop-Thread-VC-MFC-Example.htm


-Seetharam
From: Scott McPhillips [MVP] on
"Frank" <jerk(a)gmx.de> wrote in message
news:99c68d59-60bc-44f1-87b6-6430b2d6d8fc(a)b18g2000vbl.googlegroups.com...
> I tried that: Had the main thread post a message
> to the thread (PostThreadMessage), in the message handler
>
> - call AfxEndThread
> - PostQuitMessage(0)
> - m_pMainWnd->PostMessage(WM_CLOSE, 0, 0)
> - m_pMainWnd->PostMesage(WM_QUIT, 0, 0)
>
> Everything closes the main program.
>
> Do I have to do something about the message queue
> of the subthread or something?

I don't understand your post. The correct approach is to do
PostQuitMessage(0) in the thread's message handler. You can find numerous
examples in the MFC samples or tutorials where this works. Or show your
code: You are leaving too much out of your explanation.

--
Scott McPhillips [VC++ MVP]