onidle question

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
spectrum
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Sat Jul 21, 2007 12:17 pm

onidle question

Post by spectrum » Fri Aug 26, 2011 9:10 pm

Dear all,

i am using OnIdle event/callback on a wxFrame to read bytes from the serial port, to avoid further threads. My actual approach is:

Code: Select all

 void MyFrame::OnIdle (wxIdleEvent &e)
{
      int i;
      if ((i=serial->Read())>0)
     { 
         --- process data and eventually update some status messages in the frame ---
     }
     wxMilliSleep(20);
     e.RequestMore();
}
This works, but i am not sure it is the correct/portable way to do it.
Also, i am looking for faster data detection, with a read interval even < 20msec.

Any help is appreciated,
thanks
regards
spectrum

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Re: onidle question

Post by JimFairway » Sat Aug 27, 2011 12:13 pm

Hi,

Have you considered using a wxTimer instead?
See http://docs.wxwidgets.org/stable/wx_wxtimer.html
This example has a timer in it: http://wiki.wxwidgets.org/Making_a_rend ... ng_a_timer

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

dlchnr
I live to help wx-kind
I live to help wx-kind
Posts: 188
Joined: Tue Jan 27, 2009 6:45 pm
Location: Germany
Contact:

Re: onidle question

Post by dlchnr » Sat Aug 27, 2011 3:09 pm

As Jim wrote, wxTimer is the solution - here the code lines, I solved the same problem:

Code: Select all

class cMainFrame : public wxFrame {
  // :
  wxTimer tim;
  bool timlock;
  // :
  // privat member functions
  // privat copy ctor and assignment operator, if these aren't defined
  cMainFrame (const cMainFrame& obj);
  cMainFrame& operator=(const cMainFrame& obj);
  // any class providing RTTI must use this macro
  DECLARE_CLASS(cMainFrame)
  // any class wishing to process wxWidgets events must use this macro
  DECLARE_EVENT_TABLE()
public:
  // ctor
  cMainFrame(cPrgData& PrgDat);
  // dtor
  virtual ~cMainFrame();
  // basic member functions
  // :          
  // member functions
  // :
  bool PollFunction();
  // :
  void FollowAuxColouring();
  // event handlers (these functions should _not_ be virtual)
  // :
  void OnTimer(wxTimerEvent& event);
  // :
};


cMainFrame::cMainFrame(cPrgData& PrgDat) :  
            wxFrame (NULL, wxID_ANY, _T(""), wxDefaultPosition, wxSize(888,748)),
            prgdata(PrgDat), startUp(PrgDat.AutoExec(), *PrgDat.pC()), 
            pBook(), pPageTRC(), pPageOUT(), pPagePRG(), 
            tim(this, wxID_ANY), timlock(false), movOnRdy(false), 
            autoMode(false), exec_1st(false), busycnt(0), traceALL(0) {
              
  // set the frame icon
  SetIcon(wxICON(pCAN_icon));

  // create the file menu
  // :

  // :

  // :
  tim.Start(20);
  Connect(tim.GetId(), wxEVT_TIMER, wxTimerEventHandler(cMainFrame::OnTimer), NULL, this); 
}


void cMainFrame::OnTimer(wxTimerEvent& event) {

  if (!timlock) {
    timlock = true;
    PollFunction();
    timlock = false;
  }
}

Post Reply