wxProcess::Open on separate thread

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
Jacek Poplawski
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Jun 20, 2011 12:03 pm

wxProcess::Open on separate thread

Post by Jacek Poplawski » Wed Feb 29, 2012 9:06 am

I am trying to run another command in the background.

I implemented everything this way:

Code: Select all

    wxProcess* process = wxProcess::Open(wxString::FromAscii(command.c_str()));

    wxInputStream* input = process->GetInputStream();
It works but on Windows my user received warning about calling wxExecute from not-main thread.
In docs I read that wxExecute/wxProcess is not thread safe.

Can you recommend any solution?

I must call command which will take some time (like 30s) and don't block normal application at that time.

Youka
Experienced Solver
Experienced Solver
Posts: 51
Joined: Thu Feb 16, 2012 2:24 pm

Re: wxProcess::Open on separate thread

Post by Youka » Wed Feb 29, 2012 9:21 am

wxExecute seems to be implented in wxProcess::Open and surely needs to run in main thread. You can't work around of that condition, so you have to tell your main thread to the right time to call it.

Detached thread:

Code: Select all

handler->QueueEvent( new wxThreadEvent );
handler is an wxEvtHandler of class OutputCtrl, running in main thread.

Main thread:

Code: Select all

BEGIN_EVENT_TABLE(OutputCtrl, wxPanel)
    ...
    EVT_THREAD(wxID_ANY, OutputCtrl::OnAfterProcess)
END_EVENT_TABLE()

...

void OutputCtrl::OnAfterProcess(wxThreadEvent& event){
    wxString cmd_str = cmd->GetValue();
    if(!cmd_str.IsEmpty())
        wxExecute(cmd_str);
}
So i solved it in past.

Jacek Poplawski
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Jun 20, 2011 12:03 pm

Re: wxProcess::Open on separate thread

Post by Jacek Poplawski » Wed Feb 29, 2012 5:34 pm

I am afraid your solution won't work on wxWidgets 2.8, and my application must work everywhere.
I think the only solution is to create wxTimer and send events every 500 miliseconds and call CanRead() inside even handler.

Post Reply