I am developing an application which contain a wxFrame and wxThread.
The thread is going to do a loop from 1 to 10 and display this number on a TextCtrl in Frame.
User can :
-Play: start the counter/resume the counter
-Pause: pause the counter
-Reset: Set counter to 0
Now I can start the thread, pause and resume it. But i get fail to reset it. Does anyone know what is wrong in my code?
Thank you
----------------------------------
code snippet
----------------------------------
[syntax="c"]
class ThreadCnt : public wxThread
{
public:
ThreadCnt(GreenFrm *frame);
// thread execution starts here
virtual void *Entry();
public:
GreenFrm *m_frame; //my Frame
};
void *ThreadCnt::Entry()
{
wxString strCnt;
for (int cnt=0; cnt< 10; cnt++) {
strCnt = wxString::Format("%d", cnt);
m_frame->m_txtCnt->SetValue(strCnt);
if ( TestDestroy() )
break;
wxThread::Sleep(1000);
}
m_frame->m_txtCnt->SetValue(wxT("Thread finished"));
return NULL;
}
[/syntax]
-----------------------
in my GreenFrm (wxFrame):
------------------------
[syntax="c"]
ThreadCnt m_thread;
void GreenFrm::OnBtnPlayClick( wxCommandEvent& event )
{
switch (m_State) {
case STATE_STOP:
m_State = STATE_RUN;
m_thread = new ThreadCnt(this);
if ( m_thread->Create() != wxTHREAD_NO_ERROR )
{
wxLogError(wxT("Can't create thread!"));
}
if ( m_thread->Run() != wxTHREAD_NO_ERROR )
{
wxLogError(wxT("Can't start thread!"));
}
break;
case STATE_RUN:
m_State = STATE_PAUSE;
if (m_thread->IsPaused())
m_thread->Resume();
break;
case STATE_PAUSE:
m_State = STATE_RUN;
if (m_thread->IsRunning())
m_thread->Pause();
break;
} //
}
//Something wrong here.
void GreenFrm::OnBtnResetClick( wxCommandEvent& event )
{
if (m_thread != NULL) {
m_thread->Delete();
m_thread = NULL;
delete m_thread;
}
}
[/syntax]
How to resume/pause and reset wxThread?
How to resume/pause and reset wxThread?
Last edited by Sirius on Mon Sep 27, 2004 8:18 am, edited 1 time in total.
Hi Sirius,
I don't see a reset of the m_State variable in the Reset function.
I assume that after the reset, the next time you press the start/stop button, the function goes mad as it assumes to be in another state than it actually is.
Best regards,
Lall
I don't see a reset of the m_State variable in the Reset function.
I assume that after the reset, the next time you press the start/stop button, the function goes mad as it assumes to be in another state than it actually is.
Best regards,
Lall
-----------------------------------
Lall
http://www.axoris.be
Axoris - Sound makes music
-----------------------------------
Lall
http://www.axoris.be
Axoris - Sound makes music
-----------------------------------
Re: How to resume/pause and reset wxThread?
I think you will be a lot better off using a wxTimer and changing the number displayed in the wxTimerEvent rather than going through the complexity of a wxThread for what you want to do.Sirius wrote:User can :
-Play: start the counter/resume the counter
-Pause: pause the counter
-Reset: Set counter to 0
Hi Sirius,
Well, you can try to add m_State = STATE_STOP; in the OnBtnResetClick function.
If you don't do that, the "case STATE_STOP:" of your OnBtnPlayClick function will never get used again.
Best regards,
Lall
Well, you can try to add m_State = STATE_STOP; in the OnBtnResetClick function.
If you don't do that, the "case STATE_STOP:" of your OnBtnPlayClick function will never get used again.
Best regards,
Lall
-----------------------------------
Lall
http://www.axoris.be
Axoris - Sound makes music
-----------------------------------
Lall
http://www.axoris.be
Axoris - Sound makes music
-----------------------------------