Threads
-
How to use TerminateThread() function in program. iam used like this but the thread is not stopped.
CWinThread* m_pNuThread; void CThrdDlg::OnButton2() { // TODO: Add your control notification handler code here CString string; m_NuStart.GetWindowText(string); if(string =="START") { m_pNuThread = AfxBeginThread(NuThread, this); m_NuStart.SetWindowText("STOP"); } else { TerminateThread(m_pNuThread, 0); m_NuStart.SetWindowText("START"); } } UINT NuThread(LPVOID lParam) { CThrdDlg *dlg = (CThrdDlg*)lParam; //int i=0; CString str; for(int i=0;i<=100;i++) { str.Format("%d",i); dlg->m_NuEBox.SetWindowText(str); Sleep(200); if(i == 100) i=0; } return 0; }
kp.Vuppala -
How to use TerminateThread() function in program. iam used like this but the thread is not stopped.
CWinThread* m_pNuThread; void CThrdDlg::OnButton2() { // TODO: Add your control notification handler code here CString string; m_NuStart.GetWindowText(string); if(string =="START") { m_pNuThread = AfxBeginThread(NuThread, this); m_NuStart.SetWindowText("STOP"); } else { TerminateThread(m_pNuThread, 0); m_NuStart.SetWindowText("START"); } } UINT NuThread(LPVOID lParam) { CThrdDlg *dlg = (CThrdDlg*)lParam; //int i=0; CString str; for(int i=0;i<=100;i++) { str.Format("%d",i); dlg->m_NuEBox.SetWindowText(str); Sleep(200); if(i == 100) i=0; } return 0; }
kp.Vuppalakrishna Vuppala wrote:
How to use TerminateThread() function in program. iam used like this but the thread is not stopped.
Thread is not stopped because you're doing a mistake on passing the first argument to
TerminateThread
function. It must be the threadHANDLE
and not the pointer to cheCWinThread class
. Hence you have to use:TerminateThread(m_pNuThread->m_hThread, 0);
BTW: as stated by the documentation you should avoid to use
TerminateThread
(it must be only the last resort), there are other ways to stop smoothly a thread, for instance setting and (added) flag in your thread loop control condition, i.e.:UINT NuThread(LPVOID lParam)
{
//...
for(int i=0; i <= 100 && bKeepRunning;i++)
//..
}Hence you can simply set
bKeepRunning=false
outside the thread to gracefully stop it. Hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
krishna Vuppala wrote:
How to use TerminateThread() function in program. iam used like this but the thread is not stopped.
Thread is not stopped because you're doing a mistake on passing the first argument to
TerminateThread
function. It must be the threadHANDLE
and not the pointer to cheCWinThread class
. Hence you have to use:TerminateThread(m_pNuThread->m_hThread, 0);
BTW: as stated by the documentation you should avoid to use
TerminateThread
(it must be only the last resort), there are other ways to stop smoothly a thread, for instance setting and (added) flag in your thread loop control condition, i.e.:UINT NuThread(LPVOID lParam)
{
//...
for(int i=0; i <= 100 && bKeepRunning;i++)
//..
}Hence you can simply set
bKeepRunning=false
outside the thread to gracefully stop it. Hope that helps. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Mr.CPallini sir , iam very Thankful to you..iam using 2 buttons in a Dailog with two threads.it works well. now iam willing two generate two buttons with Single thread.is there any idea pls share with me sir... kp.Vuppala
-
Mr.CPallini sir , iam very Thankful to you..iam using 2 buttons in a Dailog with two threads.it works well. now iam willing two generate two buttons with Single thread.is there any idea pls share with me sir... kp.Vuppala
-
Sorry I really cannot understand your requirements, please explain better. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
I think I could decode his question.:laugh: Mr.CPallini sir , He has two buttons on the dialog and when he clicks each button it makes a thread they work very well now he wants that either buttons makes a single thread is it possible and if yes do you have any idea? (what do you think it was a good decode;))