C++ WxWidgets:跨多线程将标准输出重定向到 wxTextCtrl

发布于 2024-07-26 12:18:04 字数 645 浏览 6 评论 0原文

我的应用程序是一个多线程应用程序(使用wxThreads)。 目前,主线程及其子工作线程正在向 Stdout(使用 cout)输出各种消息。

我有一个带有 wxTextCtrl 的新框架/窗口,并且希望将所有 StdOut 消息重定向到其中。

GuiLogFrame         *logframe;

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

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

这是行不通的。 但是,如果我替换最后一行,

wxStreamToTextRedirector redir(logframe->get_log_textctrl());

只要 redir 在范围内,标准输出就会被重定向到日志框架 wxTextCtrl...我希望它保持不变,即使它超出了范围。

我想要的是 wxStreamToTextRedirector 在应用程序运行的整个过程中保持完整......所以即使新线程的 cout 也会重定向到同一个 wxTextCtrl。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-08-02 12:18:04

需要了解的一件非常重要的事情是 GUI 操作只能在主线程上完成; 如果不这样做,当您同时进行多个 GUI 操作时,它会崩溃或锁定。 这在windows下确实如​​此,但我相信它适用于所有平台。 您需要做的是使用 GetEventHandler()->AddPendingEvent 将事件发布到控件。 然后wx会将事件添加到对象的队列中,当主线程运行时,它可以进行GUI操作。

这可能不是您问题的确切答案,但它是相关信息。

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.

別甾虛僞 2024-08-02 12:18:04

我找到了一种方法,但我没有在多线程应用程序中尝试。

您可以使用 new 在 constructor() 上创建指向 wxStreamToTextRedirector 的指针。
然后,不要忘记在析构函数中删除指针。

I found one way of doing it but I didn't try it out in a multi-threaded app.

You can create a pointer to the wxStreamToTextRedirector on a constructor() using new.
And then, don't forget to delete the pointer in the destructor.

不可一世的女人 2024-08-02 12:18:04

wxStreamToTextRedirector 是一个 RAII 类,它将流与其 ctor 中的文本控件关联起来,并在其 dtor 中中断关联。 当然,您可以在堆上创建它,而不是将其用作局部变量,甚至只是手动执行其 ctor/dtor 的操作,即调用 ostr.rdbuf(text) 并恢复原始值稍后某个时间 rdbuf() 的值。

但是,正如 arolson101 所写,如果您允许多个线程使用相同的 wxTextCtrl ,那么您的代码中仍然可能会出现问题,并且简单的重定向无法帮助您解决此问题。 您需要编写自己的自定义 streambuf 派生类,这将避免立即输出文本,而是向主 GUI 线程发布一条消息,要求它执行此操作,这并非完全微不足道。

wxStreamToTextRedirector is a RAII class associating the stream with the text control in its ctor and breaking the association in its dtor. You can, of course, create it on the heap instead of using it as a local variable or even just manually do what its ctor/dtor do, i.e. call ostr.rdbuf(text) and restore the original value of rdbuf() at some later time.

However, as arolson101 wrote, you'd still could have a problem in your code if you allow multiple threads use the same wxTextCtrl and simple redirection won't help you with this. You would need to write your own custom streambuf-derived class which would avoid outputting the text immediately but post a message to the main GUI thread asking it to do it, which is not completely trivial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文