Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Threading problem

Posted on 2000-03-04
7
Medium Priority
?
372 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

609 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