demonstrates how a thread can update ui with xtd::forms::control::invoke method.
- Windows
-
- macOS
-
- Gnome
-
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/list_box>
#include <xtd/threading/thread>
class form_thread :
public form {
public:
form_thread() {
text(
"Form and thread example");
form_closed += [&] {
closed = true;
thread::join_all();
};
messages.parent(*this);
thread::start_new([&] {
auto thread_name =
ustring::format(
"thread {}", thread::current_thread().managed_thread_id());
thread::current_thread().name(thread_name);
while (!closed) {
++counter;
messages.begin_invoke([&, counter, thread_name] {
messages.items().push_back(
ustring::format(
"{}: counter: {}", thread_name, counter));
messages.selected_index(messages.items().size() - 1);
});
}
});
}
}
private:
bool closed = false;
};
auto main()->int {
}