Re: Batch file and MFC (Properly Terminating Application)
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 07 Feb 2007 02:34:17 -0500
I missed a couple things, see below...
On Wed, 07 Feb 2007 02:06:03 -0500, Joseph M. Newcomer <newcomer@xxxxxxxxxxxx> wrote:
On 6 Feb 2007 14:28:48 -0800, "one-trick-pony" <worldofpain.aamir@xxxxxxxxx> wrote:*****
// dummy1Dlg.cpp : implementation file****
//
#include "stdafx.h"
#include "dummy1.h"
#include "dummy1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDummy1Dlg dialog
CDummy1Dlg::CDummy1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CDummy1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDummy1Dlg)
m_display = _T("");
m_show = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in
Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDummy1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDummy1Dlg)
DDX_Text(pDX, IDC_EDIT1, m_display);
DDX_Text(pDX, IDC_EDIT2, m_show);
Use nice names like IDC_DISPLAY and IDC_SHOW instead of the silly ones created by the
dialog editor
****
//}}AFX_DATA_MAP****
}
BEGIN_MESSAGE_MAP(CDummy1Dlg, CDialog)
//{{AFX_MSG_MAP(CDummy1Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDCANCEL, OnClose)
It is probably a mistake to have the ON_BN_CLICKED(IDCANCEL) handler. The Cancel button
should be deleted anyway.
****
//}}AFX_MSG_MAP*****
ON_MESSAGE(UWM_MYMSG, OnMsg)
ON_MESSAGE(UWM_AUTORUN, AutoRun)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDummy1Dlg message handlers
BOOL CDummy1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this
automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
PostMessage(UWM_AUTORUN); <<< JOSEPH SUGGESTION
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code
below
// to draw the icon. For MFC applications using the document/view
model,
// this is automatically done for you by the framework.
void CDummy1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the
user drags
// the minimized window.
HCURSOR CDummy1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// HANDLES UWM_MYMSG
LRESULT CDummy1Dlg::OnMsg()
WRONG!
LRESULT CDummy1Dlg::OnMsg(WPARAM, LPARAM)
There is absolutely positively no flexibility here; you MUST specify WPARAM and LPARAM.
This is not an option, you may not omit them!
*****
{*****
m_display += "got here" ;
m_display += "\r\n";
UpdateData(FALSE);
Use a control variable and do
c_Display.SetWindowText(_T("got here"));
why do you need \r\n. (Hint: don't use edit controls to display output. They are a
massive pain to use. See my LoggingListbox example for a better approach)
*****
return 1;*****
Since you are doing PostMessage, the value is ignored. Return 0.
*****
}*****
// MY DEFINED MESSAGE HANDLER FOR UWM_AUTORUN;
/********************************************************************************
* CDummy1Dlg::AutoRun
* Inputs:
* WPARAM: ignored
* LPARAM: ignored
* Result: LRESULT
* Logically void, 0, always
* Effect:
* Launches the thread that actually executes the script
********************************************************************************/
This is the MINIMAL acceptable comment for a user-defined message handler. You can use
other formatting conventions, but ALL THIS INFORMATION MUST BE PROVIDED AT THIS SITE.
Any other approach results in an unmaintainable mess.
*****
LRESULT CDummy1Dlg::AutoRun()*****
NO!!!!
LRESULT CDummy1Dlg::AutoRun(WPARAM, LPARAM)
There is no other valid way to code this!
*****
{****
running = TRUE; // volatile BOOL running declared in dialog class
myWorkerThread = AfxBeginThread(RunCommands, this,
THREAD_PRIORITY_NORMAL, // default: use it
0, // default stack size
CREATE_SUSPENDED);
myWorkerThread->m_bAutoDelete = FALSE;
myWorkerThread->ResumeThread();
return 1;
You are posting the message. Return 0. Nobody looks at the value, so giving it other
than 0 is only confusing.
*****
}
class CTerminateThreadException : public CExeption { };
*****
*****//
UINT CDummy1Dlg::RunCommands(LPVOID p)
{
CDummy1Dlg * self = (CDummy1Dlg *)p;
try {
*****
*****self->RunCommands();
}
catch(CTerminateThreadException * e)
{
e->Delete();
}
*****return 1;
Return 0! STOP RETURING 1!
*****
*****}
void CheckAbort()
{
if(!running)
throw new CTerminateThreadException;
}
****
Joseph M. Newcomer [MVP]// THREAD-Remember it must not interact with any GUI objects!*****
int CDummy1Dlg::RunCommands()
{
double p = 0;
while (p <64002)
{
CheckAbort();
*****
p = p + 0.1;*****
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (p <64002)
{
CheckAbort();
*****
p = p + 0.1;*****
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (running && p <62002)
{
CheckAbort();
*****
p = p + 0.001;*****
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (running && p <62002)
{
CheckAbort();
*****
p = p + 0.001;*****
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
return 1;
Stop returning 1 when the value has no meaning. Return 0. Otherwise someone might
suspect that '1' in fact has meaning, when it has no meaning whatsoever.
*****
}****
void CDummy1Dlg::OnClose()
{
// TODO: Add your control notification handler code here
running = FALSE;
WaitForSingleObject(myWorkerThread->m_hThread,INFINITE);
Did you see if the WFSO exited with WAIT_OBJECT_0? Why not?
switch(WaitForSingleObject(myWorkerThread->m_hThread, INFINITE))
{ /* WFSO */
case WAIT_OBJECT_0:
*****
delete myWorkerThread; <<PROGRAM CRASHES HERE*****
break;
case WAIT_FAILED:
{
DWORD err = ::GetLastError();
ASSERT(FALSE);
break;
}
default:
ASSERT(FALSE);
break;
} /* WFSO */
******
CDummy1Dlg::OnClose();Joseph M. Newcomer [MVP]
}
If you see two posts, please disregard the other one-its a mistake.
Program crashes when it tries to execute;
delete myWorkerThread
I am following what you guys suggested. If I comment out the above
line of code, program doesnt crash. Need Help. Thanks
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- 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: Joseph M . Newcomer
- Re: Batch file and MFC (Properly Terminating Application)
- From: one-trick-pony
- 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: Joseph M . Newcomer
- Re: Batch file and MFC (Properly Terminating Application)
- Prev by Date: Re: mfc pitfalls
- Next by Date: Re: How to export afunction in class?
- Previous by thread: Re: Batch file and MFC (Properly Terminating Application)
- Next by thread: Re: Batch file and MFC (Properly Terminating Application)
- Index(es):