Re: Batch file and MFC (Properly Terminating Application)

Tech-Archive recommends: Fix windows errors by optimizing your registry



Let me first point out that I wouldn't do it the way you are doing it.

If I was to create something that executes like a batch file, I would keep
the workload and the dialog box in separate threads (that's if you even need
a dialog). If you don't need a dialog then simply do everything in
InitInstance of your application and return FALSE;

Anyway, it seems like your AutoRun has a loop in it. If that is the case
then you should probably do something like this.

CDummyDialog::CDummyDialog()
:CDialog(...)
, m_Exit(FALSE);
{
}

BOOL CDummyDlg::OnInitDialog()
{
CDialog::OnInitDialog();

//timer just to let the dialog display before I bog the thing down with
a loop
SetTimer(100,10,NULL);

return FALSE;
}

void CDummyDlg::OnTimer(..)
{
if (nIDEvent == 100)
{
KillTimer(100);
AutoRun();
return;
}
CDialog::OnTimer(...);
}

//if it has a loop
void CDummyDlg::AutoRun()
{
while (!m_Exit)
{
Do something
}
EndDialog(ID_CANCEL);
}


//if it doesn't, May I say that I would seriously dislike anyone who would
write code this way :)
void CDummyDlg::AutoRun()
{
DoSomething(...);
if (m_Exit)
{
EndDialog(ID_CANCEL);
return;
}
DoSomething2(...);
if (m_Exit)
{
EndDialog(ID_CANCEL);
return;
}
DoSomething3(...);
if (m_Exit)
{
EndDialog(ID_CANCEL);
return;
}
}

void CDummyDialog::OnCancel()
{
m_Exit = TRUE;
}

But seriously look into threads.

AliR.


"one-trick-pony" <worldofpain.aamir@xxxxxxxxx> wrote in message
news:1170427520.661905.299820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Greetings,

This is continuation of previous post listed under Subject: Batch file
and MFC. This is how basically I implemented my MFC based program to
mimic batch file execution behavior. But challenge is how to properly
terminate it, when user clicks Cancel or Exit button.

BOOL CDummyDlg::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
BOOL result = AutoRun( ); // mimics
batch file behavior
if (result == FALSE)
{
EndDialog(1);
}
Foo( );
Test( );
return TRUE; // return TRUE unless you set the focus to a
control
}

Bool CDummyDlg::AutoRun()
{
// Execute User Code
...
...
// Abort auto execution if an error occurs
if ( FileMissing == TRUE)
return FALSE; // Scott Solution-MVP suggested this
solution it works
...
...
// User clicks on Cancel button to exit (at any time)
/* This is where I have trouble-Upon OnCancel function(below)
execution, SendMessage */
/* and PostMessage are executed but after that OnCancel()
execution program returns */
/* back to execute code
below.
*/

...
...
...

return TRUE;
}

void CDummyDlg::OnCancel( )
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();

SendMessage(WM_CLOSE);
PostMessage(WM_QUIT);
// This should end the execution of program but instead
execution continues in AutoRun()
// function. And after AutoRun is done Foo() and Test() are
executed and then program
// terminates. Main or overlapped program window disappears but
code continues to run.
}


I hope this helps in understanding the problem. I would appreciate
any good software engineering solution. Thanks.



.


Quantcast