xtd 0.2.0
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) {
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());
}
static uint32 processor_count()
Gets the number of processors on the current machine.
static void run()
Begins running a standard application message loop on the current thread, without a form.
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
static thread & current_thread() noexcept
Gets the currently running thread.
static thread start_new(const xtd::threading::thread_start &start)
Create and immedialtely start a xtd::threading::thread with specified method.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
@ u
The U key.
Definition console_key.hpp:128
@ fill
All the control's edges are docked to the all edges of its containing control and sized appropriately...
Definition dock_style.hpp:35
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:217
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8