views:

614

answers:

1

My application is a multi threaded app (using wxThreads). At the moment, the main thread along with it's child worker threads are outputting various messages to Stdout (using cout).

I have a new frame/window with a wxTextCtrl, and would like to redirect all the StdOut messages in to it.

GuiLogFrame         *logframe;

logframe = new GuiLogFrame(NULL, wxID_ANY, wxEmptyString);
logframe->Show();

logredirector = new wxStreamToTextRedirector(logframe->get_log_textctrl());

This doesn't work. But if I replace the last line

wxStreamToTextRedirector redir(logframe->get_log_textctrl());

The standard out will be redirected to the logframe wxTextCtrl as long as redir is in scope... I want it to stay even when it goes out of scope.

What I want is the wxStreamToTextRedirector to stay intact the entire time the application is running... so even the new thread's cout will also redirect in to the same wxTextCtrl.

Any thoughts?

+1  A: 

One thing that is very important to know is that GUI operations should only be done on the main thread; if you don't, it will crash or lock up when you have more than one GUI operation happening at the same time. This is definitely true under windows, but I believe it applies to all platforms. What you will need to do is post an event to the control using GetEventHandler()->AddPendingEvent. Then wx will add the event to the object's queue and when the main thread runs, it can do the GUI operation.

This might not be the exact answer to your question, but it is relevant information.

arolson101