Solved

Threading problem

Posted on 2000-03-04
7
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 92
bigDiff challenge 17 142
wordappend challenge 8 247
Updating statistics with error notification email in SQL server 4 126
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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