|
1. Show us the relevant code.
2. Never use TerminateThread() .
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
|
|
|
|
|
As Rajesh said, never never use the TerminateThread function, unless you perfectly know what you are doing. Here[^] is an excellent article about threads, a bit long but it is definitively something to read if you want to start using threads.
Taken from the article:
Three functions immediately present themselves for purposes of pausing or shutting down a thread: the SuspendThread and ResumeThread methods (and their underlying API calls, ::SuspendThread and ::ResumeThread) and ::TerminateThread. Assume, for all practical purposes, except in some very limited contexts, these functions do not exist. Using them will almost always get you in trouble.
|
|
|
|
|
The better way would be to set a stop-variable and check its value regularly during the thread execution. When it's set to 1, the thread is in control of what to do to terminate.
This way, the thread has a chance to clean up memory and terminate properly.
As for the direct problem of why TerminateThread won't work, does your handle have the access rights to terminate it?
modified 13-Sep-18 21:01pm.
|
|
|
|
|
donot call the method TerminateThread
use the global memeber variable . BOOL isRunning
check the variable isRunning in the loop. if FALSE exit the thread.
|
|
|
|
|
Hi, ALL
I am using VC++ 6.0. I created a single document MFC application. I did not add any new codes to the project. After compiling, the program works fine.
To learn the execution procedure, i set some break points within
BOOL CxxxApp::InitInstance(), CxxxDoc::OnNewDocument() and CxxxView::OnInitialUpdate().
I run the application in debug mode.
I observe that the program first goes into InitInstance(). After exeuting if(!
ProcessShellCommand(cmdInfo)) within InitInstance(), the program goes into CxxxDoc,then into
CxxxView. After that, the program goes back into InitInstance() again to execute the remaining two statements
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
I tried but failed to find the intrinsic reason. What makes the program to run in this manner.
I found nowhere CxxxDoc::OnNewDocument() and CxxxView::OnInitialUpdate() are called explicitly
BOOL CxxxApp::InitInstance()
{
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif
SetRegistryKey(_T("Local...));
LoadStdProfileSettings();
CSingleDocTemplate * pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CxxxDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CxxxView));
AddDocTemplate(pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo))<----
return FALSE;
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
}
Would you please recommand some reference books or articles on this to me.
modified on Tuesday, December 14, 2010 8:20 AM
|
|
|
|
|
There are a lot of things here to explain. I suggest you to either buy a book or read MSDN. However to answer you,
What you see is what happens when you have the doc/view architecture. Try removing it from the project creation process and you will get a completely different set of code and code flow.
WRT this particular instance that you are asking about, the steps are pretty simple actually. A document template is created and marked. The ProcessShellCommand(...) does some bit of processing that creates a document, frame (with menu etc) and the view. Look into that function. The CCommandLineInfo constructor initiates the operation to CCommandLineInfo::FileNew . Then the created window is shown and asked to repaint itself with the call to UpdateWindow() And it all begins in InitInstance() because that is the starting point for the actual creation of the application/process.
|
|
|
|
|
cy163@hotmail.com wrote: I found nowhere CxxxDoc::OnNewDocument() and CxxxView::OnInitialUpdate() are called explicitly
They are called from within the MFC framework.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
|
It can be posible to (bubble) sort an ObList after some criteria ?
Here I have :
typedef CTypedPtrList<<CObList, CDrawObj*>> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
ObjList.AddTail(pObj)
}
ok , by now seems to be 5 element in objects array
My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition();
while(pos)
{
CDrawObj* pObj1 = ObjList.GetNext(pos);
if(pos == NULL)break;
CDrawObj* pObj2 = ObjList.GetNext(pos);
if(pObj1->m_position.left > pObj2->m_position.left)
}
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
|
|
|
|
|
Here is a useful article on CodeProject to get you started: Using the std::sort() Method[^].
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
The article sorts a STL collection class, but this just complement my answer
|
|
|
|
|
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
|
|
|
|
|
Well , I must change much code to use std::sort ( plus , I never used before ) , it is the only way ? I mean , how can I switch position between two elements of an objects array ?
|
|
|
|
|
mesajflaviu wrote: plus , I never used before
This is just a bad excuse . The STL collection classes are much more powerful than the MFC collection classes, I strongly suggest you start using them instead of the MFC collections, you won't regret it (it takes a bit of time to get started with it but once you know how to use them, you will gain a lot of time).
mesajflaviu wrote: I mean , how can I switch position between two elements of an objects array ?
You can still remove an element and insert it back at the correct location but this is ugly and prone to errors. I'm not aware of another alternative (but I didn't use the MFC collections for long).
|
|
|
|
|
I think that I try in both way .. thanks !
|
|
|
|
|
Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL.
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
|
|
|
|
|
If I mix MFC with STL , I wouldn't keep consistency , or not ?
|
|
|
|
|
It's been a while (and I don't have my book with me) but iirc, the std::algorithm (and I think sort is in that) didn't require the structure to be a std::list. (I seem to recall standard arrays being able to be sorted with std as well.)
|
|
|
|
|
// switch pObj1 and pObj2 between them ... but how ?
Try it :
void SortDrawObjects(CDrawObjList& cList)
{
for (int i = 0; i < cList.GetCount(); i++) {
POSITION pos = cList.GetHeadPosition();
while (pos) {
POSITION posFirst = pos;
CDrawObj* pcFirst = cList.GetNext(pos);
if (pos) {
POSITION posSecond = pos;
CDrawObj* pcSecond = cList.GetNext(pos);
if (pcFirst->m_position.left > pcSecond->m_position.left) {
cList.SetAt(posFirst, pcSecond);
cList.SetAt(posSecond, pcFirst);
}
pos = posSecond;
}
}
}
}
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
Well , it's work great ! Thank you very much !
|
|
|
|
|
An algorithm after all is just a modus operandi. As long as a collection class, irrespective of the library that is part of, provides mechanism to insert and remove elements at any location, all the algorithms can be implemented. The performance may be not as expected due to the implementation of the classes themselves.
What I am trying to say is, if you understand the algorithm and read the documentation for the collection class, it must not be difficult at all.
|
|
|
|
|
|
|
If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint.
For instance ...
typedef CTypedPtrArray<<CObArray, CDrawObj*>> CDrawObjArray;
then create the array ...
CDrawObjArray ObjArray;
const unsigned int nSize = 5;
ObjArray.SetSize(nSize);
fill the array ...
for(int i = 0; i < nSize; i++)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
ObjArray[i] = pObj;
}
and finally sort the array (using the most inefficient sorting algorithim known to man).
for(int i = 0; i < nSize; i++)
{
for(int j = i; j < nSize; j++)
{
unsigned int nPrevious = ObjArray[j-1]->m_position.left;
unsigned int nCurrent = ObjArray[j]->m_position.left;
if(nPrevious > nCurrent)
{
CDrawObj* pObj = ObjArray[j-1];
ObjArray[j-1] = ObjArray[j];
ObjArray[j] = pObj ;
}
}
}
modified 17-Jan-19 15:57pm.
|
|
|
|
|
Michael Waters wrote: ...(using the most inefficient sorting algorithim known to man).
I can think of one worse.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|