Re: Batch file and MFC (Properly Terminating Application)



// 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);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDummy1Dlg, CDialog)
//{{AFX_MSG_MAP(CDummy1Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDCANCEL, OnClose)
//}}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()
{
m_display += "got here" ;
m_display += "\r\n";
UpdateData(FALSE);
return 1;
}

// MY DEFINED MESSAGE HANDLER FOR UWM_AUTORUN;
LRESULT CDummy1Dlg::AutoRun()
{
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;
}

//
UINT CDummy1Dlg::RunCommands(LPVOID p)
{
CDummy1Dlg * self = (CDummy1Dlg *)p;
self->RunCommands();
return 1;
}

// THREAD-Remember it must not interact with any GUI objects!
int CDummy1Dlg::RunCommands()
{

double p = 0;
while (p <64002)
{
p = p + 0.1;
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (p <64002)
{
p = p + 0.1;
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (running && p <62002)
{
p = p + 0.001;
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}
p = 0;
while (running && p <62002)
{
p = p + 0.001;
if (floor(p) / 1000 == 10) PostMessage(UWM_MYMSG);
}

return 1;
}

void CDummy1Dlg::OnClose()
{
// TODO: Add your control notification handler code here

running = FALSE;
WaitForSingleObject(myWorkerThread->m_hThread,INFINITE);
delete myWorkerThread; <<PROGRAM CRASHES HERE
CDummy1Dlg::OnClose();

}

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

.


Loading