EricNielsen
April 29th, 1999, 04:54 PM
In a dialog-based application I have a main routine that
creates a new thread to run a lengthy calculation process. By
trapping certain keystokes, I want to enabled a means for the
user to stop the calculation after it has started.
It has been suggested to me to add a WaitForSingleObject()
call to my CalculationProcess() routine. If I do that, what
handle do I give it? How do I signal the process to terminate?
I have annotated the following code snippets with
"<<<<<<Need help here" where I have questions.
Thanks for any help!!
Eric
================================================================
void CVCDevDlg::OnOK()
{
// <<snipped code>>
if(!calcProc) //calcProc is a member variable
calcProc = AfxBeginThread(CalculationProcess, this);
// <<snipped code>>
}
BOOL CVCDevDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(IsCancelMsg(pMsg))
{
if(AfxMessageBox("Terminate analysis ??",
MB_ICONQUESTION|MB_YESNO) == IDYES)
{
//kill calcProc thread -- how?? <<<<<<Need help here
AfxMessageBox("Analysis terminated !!",
MB_ICONEXCLAMATION);
return 0;
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CVCDevDlg::IsCancelMsg(MSG* pMsg)
{
//If calcProc is not running, then pMsg is not a CANCEL message
if(calcProc == NULL)
return FALSE;
//Examine pMsg to determine is it is a CANCEL message
if((pMsg->wParam == 67 && GetKeyState(VK_CONTROL) & 0x8000) ||
pMsg->wParam == VK_ESCAPE)
return TRUE;
return FALSE;
}
UINT CalculationProcess(LPVOID pParam)
{
CVCDevDlg* pDlg = (CVCDevDlg*)pParam;
CString str;
if (pDlg == NULL)
return -1; // illegal parameter
for(long i=0; i<3000000; i++)
{
//My real code runs a database analysis, but
//for this example, let's just count to 3M
str.Format("%d", i);
pDlg->SetDlgItemText(IDC_STATIC_STATUS, str);
//<<<<<<Need help here
// What handle do I give
// to wait call?
// Am I checking for the
// correct return value?
if(WaitForSingleObject(hObj, 0) == WAIT_OBJECT_0)
{
AfxEndThread(-1);
}
}
pDlg->calcProc = NULL;
return 0; // thread completed successfully
}
Eric
creates a new thread to run a lengthy calculation process. By
trapping certain keystokes, I want to enabled a means for the
user to stop the calculation after it has started.
It has been suggested to me to add a WaitForSingleObject()
call to my CalculationProcess() routine. If I do that, what
handle do I give it? How do I signal the process to terminate?
I have annotated the following code snippets with
"<<<<<<Need help here" where I have questions.
Thanks for any help!!
Eric
================================================================
void CVCDevDlg::OnOK()
{
// <<snipped code>>
if(!calcProc) //calcProc is a member variable
calcProc = AfxBeginThread(CalculationProcess, this);
// <<snipped code>>
}
BOOL CVCDevDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(IsCancelMsg(pMsg))
{
if(AfxMessageBox("Terminate analysis ??",
MB_ICONQUESTION|MB_YESNO) == IDYES)
{
//kill calcProc thread -- how?? <<<<<<Need help here
AfxMessageBox("Analysis terminated !!",
MB_ICONEXCLAMATION);
return 0;
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CVCDevDlg::IsCancelMsg(MSG* pMsg)
{
//If calcProc is not running, then pMsg is not a CANCEL message
if(calcProc == NULL)
return FALSE;
//Examine pMsg to determine is it is a CANCEL message
if((pMsg->wParam == 67 && GetKeyState(VK_CONTROL) & 0x8000) ||
pMsg->wParam == VK_ESCAPE)
return TRUE;
return FALSE;
}
UINT CalculationProcess(LPVOID pParam)
{
CVCDevDlg* pDlg = (CVCDevDlg*)pParam;
CString str;
if (pDlg == NULL)
return -1; // illegal parameter
for(long i=0; i<3000000; i++)
{
//My real code runs a database analysis, but
//for this example, let's just count to 3M
str.Format("%d", i);
pDlg->SetDlgItemText(IDC_STATIC_STATUS, str);
//<<<<<<Need help here
// What handle do I give
// to wait call?
// Am I checking for the
// correct return value?
if(WaitForSingleObject(hObj, 0) == WAIT_OBJECT_0)
{
AfxEndThread(-1);
}
}
pDlg->calcProc = NULL;
return 0; // thread completed successfully
}
Eric