Re: Batch file and MFC (Properly Terminating Application)
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 05 Feb 2007 22:54:19 -0500
Actually, he's only waiting at the exit routine. The problem is that he hasn't said how
he got the hThread (e.g., it can'tbe the m_hThread member of the CWinThread * object
unless m_bAutoDelete is set or it is actual a duplicate handle, but I want to hear his
explanation of where it came from)
joe
On Mon, 05 Feb 2007 19:09:53 -0500, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
wrote:
one-trick-pony wrote:Joseph M. Newcomer [MVP]
Thanks Scott-
afx_msg LRESULT OnDummyMsg(WPARAM wParam, LPARAM lParam );
I wanted to keep things simple by not using any input parameters but
shortcuts can be dangerous. I have a working program with thread that
is using Joseph's code for thread creation. Now next issue, I am
working on terminating threads. Once thread is spun off and main gui
is sitting and waiting for thread to finish, user clicks Exit on main
GUI thread.
You have a couple of contradictions here. The GUI thread should not be
waiting for a thread it just started: That defeats the purpose. And
worse, if the GUI thread is waiting for the thread it will not respond
to the user clicking anything.
Below is skeleton of the thread code;
UINT WorkerThreadProc( LPVOID Param ) //Sample function for using in
AfxBeginThread
{
CFile file;
file.Open("C:\\Temp\\test.txt",CFile::modeCreate|CFile::modeWrite);
CString strValue;
for(int i=0;i<=100;i++)
{
strValue.Format("Value:%d",i);
file.Write(strValue,strValue.GetLength());
}
file.Close();
// more code below
...
...
...
...
return TRUE;
}
void CDummyDlg::OnExit() // Exit button handler
{
running = FALSE; // could use this to signal thread but I have to
check for it after each instruction or line of code-not good
WaitForSingleObject(hThread, INFINITE) ; // Don't wanna wait for
thread to finish must exit on user request !
CDialog::OnCancel( );
}
Now, assume thread is executing, user clicks Exit on Main GUI thread.
How can I signal above thread code to stop whatever you are doing and
exit? It is not a good software engineering to check for a Exit
condition (ie, running == FALSE) after each line of code inside thread
function.
You don't need to check after each line of code. The user won't be
aware of nanoseconds, microseconds, or milliseconds of delay. You just
need to check before resuming lengthy operations:
for(int i=0; i<=100 && running; i++)
{
strValue.Format("Value:%d",i);
file.Write(strValue,strValue.GetLength());
}
file.Close();
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Batch file and MFC (Properly Terminating Application)
- From: one-trick-pony
- Re: Batch file and MFC (Properly Terminating Application)
- References:
- Re: Batch file and MFC (Properly Terminating Application)
- From: Joseph M . Newcomer
- Re: Batch file and MFC (Properly Terminating Application)
- From: Alexander Grigoriev
- Re: Batch file and MFC (Properly Terminating Application)
- From: Joseph M . Newcomer
- Re: Batch file and MFC (Properly Terminating Application)
- From: one-trick-pony
- Re: Batch file and MFC (Properly Terminating Application)
- From: Scott McPhillips [MVP]
- Re: Batch file and MFC (Properly Terminating Application)
- From: one-trick-pony
- Re: Batch file and MFC (Properly Terminating Application)
- From: Scott McPhillips [MVP]
- Re: Batch file and MFC (Properly Terminating Application)
- From: one-trick-pony
- Re: Batch file and MFC (Properly Terminating Application)
- From: Scott McPhillips [MVP]
- Re: Batch file and MFC (Properly Terminating Application)
- Prev by Date: Re: Batch file and MFC (Properly Terminating Application)
- Next by Date: Re: CFont Guidance
- Previous by thread: Re: Batch file and MFC (Properly Terminating Application)
- Next by thread: Re: Batch file and MFC (Properly Terminating Application)
- Index(es):
Loading