1

ここには、ウィンドウ用のクラス (Nana C++ を使用) とネットワーク用のいくつかのスレッドで構成されるコードがいくつかあります。ただし、ユーザーに出力することはできないようです。メッセージボックスを使用してテキストボックスに追加し、コンソールに出力しようとしましたが、表示されません。これは Nana または Boost.Thread の問題ですか?

Boost.Thread に問題がある場合は、std::thread に切り替えることができますが、うまくいかないと思います。

void thread()//Let's say this function is started on a thread and the window is started on main
{
    append(L"append test");
    MsgBox(L"msgbox test"):
}
4

1 に答える 1

2

他のスレッドにテキストを追加する方法を示すナナのデモがあります。

#include <nana/gui.hpp>
#include <nana/gui/widgets/textbox.hpp>
#include <nana/gui/widgets/button.hpp>
#include <mutex>
#include <condition_variable>
#include <thread>

int main()
{
    using namespace nana;

    form fm(API::make_center(300, 250));

    textbox txt(fm, rectangle(10, 10, 280, 190));
    button btn(fm, rectangle(10, 220, 200, 20));
    btn.caption("append texts in other thread");

    std::mutex mutex;
    std::condition_variable condvar;
    volatile bool running = true;

    std::thread thrd([&]
    {
        while(running)
        {
            std::unique_lock<std::mutex> lock(mutex);
            condvar.wait(lock);

            txt.append(L"append a new line\n", false);

            msgbox mb(L"hello");
            mb<<L"This is a demo";
            mb.show();
        }
    });

    btn.events().click([&]
    {
        std::lock_guard<std::mutex> lock(mutex);
        condvar.notify_one();
    });

    fm.events().unload([&]
    {
        running = false;
        std::lock_guard<std::mutex> lock(mutex);
        condvar.notify_one();   
    });

    fm.show();
    exec();
    thrd.join();
}

このデモは Nana C++ Library 1.0.2 で作成され、Windows/Linux で正常に動作します

于 2015-05-19T18:14:00.817 に答える