有一个 Nana 演示说明如何在其他线程中附加文本。
#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 上运行良好