Solved

Threading problem

Posted on 2000-03-04
7
370 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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