Solved

Threading problem

Posted on 2000-03-04
7
365 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to include alternate header placement and naming 5 66
Folder Comparison 12 53
conditional code and condition difference 9 77
EvenOdd challenge 10 90
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

20 Experts available now in Live!

Get 1:1 Help Now