virtual bool OnInit();
};
+// Define an array of process pointers used by MyFrame
+class MyPipedProcess;
+WX_DEFINE_ARRAY(MyPipedProcess *, MyProcessesArray);
+
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
void OnAbout(wxCommandEvent& event);
+ // polling output of async processes
+ void OnIdle(wxIdleEvent& event);
+
// for MyPipedProcess
+ void OnProcessTerminated(MyPipedProcess *process)
+ { m_running.Remove(process); }
wxListBox *GetLogListBox() const { return m_lbox; }
private:
wxListBox *m_lbox;
+ MyProcessesArray m_running;
+
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
};
MyPipedProcess(MyFrame *parent, const wxString& cmd)
: MyProcess(parent, cmd)
{
- m_needPipe = TRUE;
+ Redirect();
}
virtual void OnTerminate(int pid, int status);
+
+ bool HasInput();
};
// ----------------------------------------------------------------------------
EVT_MENU(Exec_DDEExec, MyFrame::OnDDEExec)
EVT_MENU(Exec_About, MyFrame::OnAbout)
+
+ EVT_IDLE(MyFrame::OnIdle)
END_EVENT_TABLE()
// Create a new application object: this macro will allow wxWindows to create
if ( !cmd )
return;
+ wxLogStatus(_T("'%s' is running please wait..."), cmd.c_str());
+
int code = wxExecute(cmd, TRUE /* sync */);
+
wxLogStatus(_T("Process '%s' terminated with exit code %d."),
cmd.c_str(), code);
m_cmdLast = cmd;
return;
wxProcess *process = new MyProcess(this, cmd);
- if ( !wxExecute(cmd, FALSE /* async */, process) )
+ long pid = wxExecute(cmd, FALSE /* async */, process);
+ if ( !pid )
{
wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
}
else
{
+ wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str());
+
m_cmdLast = cmd;
}
}
if ( !cmd )
return;
- wxProcess *process = new MyPipedProcess(this, cmd);
- if ( !wxExecute(cmd, FALSE /* async */, process) )
+ bool sync;
+ switch ( wxMessageBox(_T("Execute it synchronously?"),
+ _T("Exec question"),
+ wxYES_NO | wxCANCEL | wxICON_QUESTION, this) )
{
- wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
+ case wxYES:
+ sync = TRUE;
+ break;
- delete process;
+ case wxNO:
+ sync = FALSE;
+ break;
+
+ default:
+ return;
}
- else
+
+ if ( sync )
{
- m_cmdLast = cmd;
+ wxArrayString output;
+ int code = wxExecute(cmd, output);
+ wxLogStatus(_T("command '%s' terminated with exit code %d."),
+ cmd.c_str(), code);
+
+ if ( code != -1 )
+ {
+ m_lbox->Append(wxString::Format(_T("--- Output of '%s' ---"),
+ cmd.c_str()));
+
+ size_t count = output.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ m_lbox->Append(output[n]);
+ }
+ }
+ }
+ else // async exec
+ {
+ MyPipedProcess *process = new MyPipedProcess(this, cmd);
+ if ( !wxExecute(cmd, FALSE /* async */, process) )
+ {
+ wxLogError(_T("Execution of '%s' failed."), cmd.c_str());
+
+ delete process;
+ }
+ else
+ {
+ m_running.Add(process);
+ }
}
+
+ m_cmdLast = cmd;
}
void MyFrame::OnDDEExec(wxCommandEvent& WXUNUSED(event))
#endif // __WINDOWS__
}
+// input polling
+void MyFrame::OnIdle(wxIdleEvent& event)
+{
+ size_t count = m_running.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ if ( m_running[n]->HasInput() )
+ {
+ event.RequestMore();
+ }
+ }
+}
+
// ----------------------------------------------------------------------------
// MyProcess
// ----------------------------------------------------------------------------
delete this;
}
-void MyPipedProcess::OnTerminate(int pid, int status)
+// ----------------------------------------------------------------------------
+// MyPipedProcess
+// ----------------------------------------------------------------------------
+
+bool MyPipedProcess::HasInput()
{
- // show the program output
- wxListBox *lbox = m_parent->GetLogListBox();
- lbox->Append(wxString::Format(_T("--- Output of '%s' ---"), m_cmd.c_str()));
+ wxInputStream& is = *GetInputStream();
+ if ( !is.Eof() )
+ {
+ wxTextInputStream tis(is);
- wxTextInputStream tis(*m_in_stream);
- while ( !m_in_stream->LastError() )
+ // this assumes that the output is always line buffered
+ wxString msg;
+ msg << m_cmd << _T(": ") << tis.ReadLine();
+
+ m_parent->GetLogListBox()->Append(msg);
+
+ return TRUE;
+ }
+ else
{
- lbox->Append(tis.ReadLine());
+ return FALSE;
}
+}
+
+void MyPipedProcess::OnTerminate(int pid, int status)
+{
+ // show the rest of the output
+ while ( HasInput() )
+ ;
+
+ m_parent->OnProcessTerminated(this);
MyProcess::OnTerminate(pid, status);
}