How to resume/pause and reset wxThread?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Sirius
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Sep 07, 2004 3:18 am
Contact:

How to resume/pause and reset wxThread?

Post by Sirius »

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]
Last edited by Sirius on Mon Sep 27, 2004 8:18 am, edited 1 time in total.
Lall
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Sep 16, 2004 12:35 pm
Location: Belgium
Contact:

Post by Lall »

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
-----------------------------------
Lall

http://www.axoris.be
Axoris - Sound makes music
-----------------------------------
Sirius
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Sep 07, 2004 3:18 am
Contact:

Post by Sirius »

Yes, cause i am not sure how do make a reset/start thread, so my code might not correct in this part, and this is the case i am asking here :-)

thank you
OS: WinXP
wxMSW 2.5.2, VC 6.0

OS: Debian sarge
wxGTK 2.4.2, gcc 3.3.3

I like Linux and C++
Mampfred
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Aug 31, 2004 2:28 pm
Location: Germany
Contact:

Re: How to resume/pause and reset wxThread?

Post by Mampfred »

Sirius wrote:User can :
-Play: start the counter/resume the counter
-Pause: pause the counter
-Reset: Set counter to 0
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.
Lall
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Sep 16, 2004 12:35 pm
Location: Belgium
Contact:

Post by Lall »

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
-----------------------------------
Lall

http://www.axoris.be
Axoris - Sound makes music
-----------------------------------
Sirius
Earned a small fee
Earned a small fee
Posts: 15
Joined: Tue Sep 07, 2004 3:18 am
Contact:

Post by Sirius »

Hi Lall,

Thanks for your reply, it is work now. I thought I did something wrong with the thread, cant believe I had miss up this thing :shock:


Thank you
:)
OS: WinXP
wxMSW 2.5.2, VC 6.0

OS: Debian sarge
wxGTK 2.4.2, gcc 3.3.3

I like Linux and C++
Post Reply