xtd 0.2.0
Loading...
Searching...
No Matches
form_and_thread.cpp

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>
using namespace xtd;
using namespace xtd::forms;
using namespace xtd::threading;
class form_thread : public form {
public:
form_thread() {
text("Form and thread example");
form_closed += [&] {
closed = true;
};
messages.parent(*this);
messages.dock(dock_style::fill);
for (auto index = 0_z; index < environment::processor_count() - 1; ++index) {
thread::start_new([&] {
auto counter = 0u;
auto thread_name = string::format("thread {}", thread::current_thread().managed_thread_id());
thread::current_thread().name(thread_name);
while (!closed) {
thread::sleep(50_ms);
++counter;
messages.begin_invoke([&, counter, thread_name] {
messages.items().push_back(string::format("{}: counter: {}", thread_name, counter));
messages.selected_index(messages.items().size() - 1);
});
}
});
}
}
private:
list_box messages;
bool closed = false;
};
auto main() -> int {
application::run(form_thread());
}
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:54
Represents a standard Windows list box.
Definition list_box.h:33
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10