Solved

Threading problem

Posted on 2000-03-04
7
363 Views
Last Modified: 2013-11-20
Ok, i am creating a user-interface thread, to accomplish that, i have a created a class that extends CWinThread, now i am using AfxBeginThread to create the thread, that works fine, except that, once i'm in the thread and do whatever i wanted to do (basically show a dialog box, do a few stuff, then kill the dialog box) i would like to end the thread, now i've tried using AfxEndThread(0) but that leads to a "Debug Assertion Failed" so i tried using PostQuitMessage(0) but that leads the WHOLE program (the thread i create AND my main thread) to close down!!!
can anyone pls tell me what i'm doing wrong here? If code is needed, pls post comment, thanks in advance

 
the thread is being created as such

AfxBeginThread(RUNTIME_CLASS(CmyThreadApp),0,0,0,NULL)
 
in a function in my main thread, and it is being (trying/failing) terminated in the thread itself
0
Comment
Question by:gkanaan
  • 5
  • 2
7 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2584293





0
 
LVL 5

Accepted Solution

by:
Wyn earned 200 total points
ID: 2584294
->do whatever i wanted to do (basically show a dialog box, do a few stuff, then kill the dialog box) i would like to end the thread, now i've tried using AfxEndThread(0) but that leads to a "Debug Assertion Failed"
===================
In general,you should let it end by itself.

If you want to end it,you can use following ways:

1:If it dispatchs messages,you could terminate the thread by posting it a WM_QUIT message, i.e. pConnection->PostThreadMessage(WM_QUIT, 0, 0)).

2:If the thread has a main window, you could post that window a WM_CLOSE message.

3:Using AfxEndThread();

4:Least choice:
TerminateThread(HANDLE hThread, DWORD dwExitCode) to forcefully terminate any thread from another.

Regards
W.Yinan
0
 

Author Comment

by:gkanaan
ID: 2584300
Excellent, the 1st solution you suggested worked perfectly, (PostThreadMessage)

Thank you
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:Wyn
ID: 2584305
Btw:how you declare CmyThreadApp ,have you added DECLARE_DYNAMIC, DECLARE_DYNCREATE, or DECLARE_SERIAL ?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2584308
quoted:
To create a thread, call AfxBeginThread. There are two forms, depending on whether you want a worker or user-interface thread. If you want a user-interface thread, pass to AfxBeginThread a pointer to the CRuntimeClass of your CWinThread-derived class. If you want to create a worker thread, pass to AfxBeginThread a pointer to the controlling function and the parameter to the controlling function. For both worker threads and user-interface threads, you can specify optional parameters that modify priority, stack size, creation flags, and security attributes. AfxBeginThread will return a pointer to your new CWinThread object.

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2584310
quoted:
To create a thread, call AfxBeginThread. There are two forms, depending on whether you want a worker or user-interface thread. If you want a user-interface thread, pass to AfxBeginThread a pointer to the CRuntimeClass of your CWinThread-derived class. If you want to create a worker thread, pass to AfxBeginThread a pointer to the controlling function and the parameter to the controlling function. For both worker threads and user-interface threads, you can specify optional parameters that modify priority, stack size, creation flags, and security attributes. AfxBeginThread will return a pointer to your new CWinThread object.

0
 

Author Comment

by:gkanaan
ID: 2584314
Yes i added
DECLARE_DYNAMIC, DECLARE_DYNCREATE
actually it's added automatically by AppWizard when you specify that your new class is an extension of CWinThread
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now