Click here to Skip to main content
15,887,464 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: good programmer Pin
AmbiguousName17-Sep-10 22:19
AmbiguousName17-Sep-10 22:19 
AnswerRe: good programmer Pin
Emilio Garavaglia18-Sep-10 21:01
Emilio Garavaglia18-Sep-10 21:01 
QuestionLoad balance problem Pin
yu-jian16-Sep-10 16:30
yu-jian16-Sep-10 16:30 
AnswerRe: Load balance problem Pin
Garth J Lancaster16-Sep-10 23:59
professionalGarth J Lancaster16-Sep-10 23:59 
QuestionOLEDB CAccessor with CMultipleResults what am I doing wrong. Pin
Spawn@Melmac16-Sep-10 12:35
Spawn@Melmac16-Sep-10 12:35 
Questiongetting names of the lines in TAPI?? Pin
AmbiguousName16-Sep-10 7:53
AmbiguousName16-Sep-10 7:53 
QuestionRe: getting names of the lines in TAPI?? Pin
David Crow16-Sep-10 10:39
David Crow16-Sep-10 10:39 
QuestionTerminating Threads! [modified] Pin
Caslen16-Sep-10 3:21
Caslen16-Sep-10 3:21 
I have an MFC MDI app in which I have included a docking control bar (Cristi Posea - CSizingControlBar - a resizable control bar) The controlbar has a dialog associated with it which, amongst other things, continuously grabs data from a PCI A/D card using a worker thread.

I have the following code,
first initialise the control bar and associated dialog:-

in MainFrm.h:-

class CMainFrame : public CMDIFrameWnd
{
	DECLARE_DYNAMIC(CMainFrame)
public:
	CMainFrame();

	CMainControlBar	m_MCB;			//the control bar
	CDataControls	m_GrabDataDialog; 	//the dialog attached to the control bar

//other stuff...
.
.
.
}



then in MainFrm.cpp:-

int CMainFrame::EnableControlBar()
{

	if (!m_MCB.Create(_T("Data Window"), this, 50))
	{
		TRACE0("Failed to create Data Window\n");
		return -1;
	}
	m_MCB.SetBarStyle(m_MCB.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC );
	m_MCB.EnableDocking(CBRS_ALIGN_ANY);

#ifdef _SCB_REPLACE_MINIFRAME
    m_pFloatingFrameClass = RUNTIME_CLASS(CSCBMiniDockFrameWnd);
#endif 

	DockControlBar(&m_MCB, AFX_IDW_DOCKBAR_LEFT);
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~  Attach Controls Dialog  
	//~~~~  to  Window if possible
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if (!m_DataDialog.Create(IDD_MAINCONTROLS,&m_MCB))
	{
		TRACE0("Failed to create instant bar child\n");
		return -1;		// fail to create
	}

	//show the bar!
	m_DataDialog.ShowWindow(SW_SHOW);

	return 0;
}



Next, set up the thread:-

class CDataControls : public CDialog
{
// Construction
public:
	virtual ~CDataControls();
	CDataControls(CWnd* pParent = NULL);   

	CGraphDraw m_Graph; //the custom control
	void DoDataDraw(); //passes data to the custom control
	void DoDataGrab(); //grabs data from an A/D converter

	BOOL m_TerminateThreads; 	//flag to terminate the thread
	CWinThread *pDataGrabThread; 	//thread pointer

	double	Sig[1024];

//other stuff...
.
.
.
};



and:-

BOOL CDataControls::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Initialise Custom Control
	m_Graph.Initialise();

	//Setup Grab Thread
	pDataGrabThread = AfxBeginThread(DataGrabThread,this,THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);		
        if(pDataGrabThread==NULL)
		AfxMessageBox(L"Crap Thread");

	pDataGrabThread->m_bAutoDelete=FALSE;
	pDataGrabThread->ResumeThread();
	
	
	return TRUE;
}



Now the actual thread:-

UINT DataGrabThread(LPVOID me)
{
	CDataControls *self = (CDataControls *)me;

	while(self->m_TerminateThreads!=0)
	{
		self->DoDataGrab(); 	//go and grab the data
		Sleep(100);		//don't hog the CPU
	}
	return 0;
}



To close the thread when the controlbar closes I have:-

void CDataControls::OnClose() 
{
	m_TerminateThreads=FALSE;  	//Signal terminate the thread

	WaitForSingleObject(pDataGrabThread->m_hThread,10000); //give it chance to stop
	delete pDataGrabThread; 	//then delete it
	
	CDialog::OnClose();
}




Now I assumed that when I close the main application that it would take care of closing the controlbar sensibly via CDataControls::OnClose() but it doesn't - CDataControls::OnClose() isn't called at all, and I get 'MFC Application has encountered a problem...' error.




So now I have the following in the MainFrm.cpp:-

void CMainFrame::OnClose() 
{
	::SendMessage(m_ControlsDialog,WM_CLOSE,0,0);  //send a message to close the dialog and thread
	
	CMDIFrameWnd::OnClose();
}


This now works perfectly! No errors, no memory leaks just apparently a nice clean finish. My question then, is this the correct way to go about this? I haven't had much experience with threads and although this seems to work well and I'm fairly happy with it I would like to know if it is the right way to do it and if not how else might I go about closing a thread running in a non-modal dialog when the main application is closed?

modified on Thursday, September 16, 2010 11:02 AM

AnswerRe: Terminating Threads! Pin
Cool_Dev16-Sep-10 5:26
Cool_Dev16-Sep-10 5:26 
GeneralRe: Terminating Threads! Pin
Roger Allen16-Sep-10 5:50
Roger Allen16-Sep-10 5:50 
GeneralRe: Terminating Threads! Pin
Luc Pattyn16-Sep-10 6:14
sitebuilderLuc Pattyn16-Sep-10 6:14 
GeneralRe: Terminating Threads! Pin
Caslen16-Sep-10 5:59
Caslen16-Sep-10 5:59 
GeneralRe: Terminating Threads! Pin
Chris Losinger16-Sep-10 6:02
professionalChris Losinger16-Sep-10 6:02 
GeneralRe: Terminating Threads! Pin
Caslen16-Sep-10 6:08
Caslen16-Sep-10 6:08 
AnswerRe: Terminating Threads! Pin
David Crow16-Sep-10 5:53
David Crow16-Sep-10 5:53 
GeneralRe: Terminating Threads! Pin
Caslen16-Sep-10 6:04
Caslen16-Sep-10 6:04 
GeneralRe: Terminating Threads! Pin
David Crow16-Sep-10 6:45
David Crow16-Sep-10 6:45 
GeneralRe: Terminating Threads! Pin
Caslen16-Sep-10 22:31
Caslen16-Sep-10 22:31 
QuestionRe: Terminating Threads! Pin
David Crow17-Sep-10 2:56
David Crow17-Sep-10 2:56 
AnswerRe: Terminating Threads! Pin
Caslen17-Sep-10 3:04
Caslen17-Sep-10 3:04 
GeneralRe: Terminating Threads! Pin
David Crow17-Sep-10 3:09
David Crow17-Sep-10 3:09 
AnswerRe: Terminating Threads! Pin
Roger Allen16-Sep-10 5:54
Roger Allen16-Sep-10 5:54 
QuestionLine Drawing Pin
Benjamin Bruno16-Sep-10 1:17
Benjamin Bruno16-Sep-10 1:17 
AnswerRe: Line Drawing Pin
Shivanand Gupta16-Sep-10 1:28
Shivanand Gupta16-Sep-10 1:28 
QuestionRe: Line Drawing Pin
David Crow16-Sep-10 5:45
David Crow16-Sep-10 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.