diff --git a/examples/xtd.core.examples/README.md b/examples/xtd.core.examples/README.md index 7cebf87a5380..75226cfcedf3 100644 --- a/examples/xtd.core.examples/README.md +++ b/examples/xtd.core.examples/README.md @@ -286,6 +286,7 @@ * [thread](threading/thread/README.md) shows hows how to use [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) class. * [thread_pool](threading/thread_pool/README.md) shows hows how to use [xtd::threading::thread_pool](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread__pool.html) class. * [timeout](threading/timeout/README.md) shows how to use [xtd::threading:timeout](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1timeout.html) class. +* [timer](threading/timeout/README.md) shows how to use [xtd::threading:timer](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1timer.html) class. * [wait_handle](threading/wait_handle/README.md) shows how to use [xtd::threading:wait_handle](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1wait__handle.html) class. ## [Uri](uri/README.md) diff --git a/examples/xtd.core.examples/threading/CMakeLists.txt b/examples/xtd.core.examples/threading/CMakeLists.txt index 5486ec997235..55401566f596 100644 --- a/examples/xtd.core.examples/threading/CMakeLists.txt +++ b/examples/xtd.core.examples/threading/CMakeLists.txt @@ -16,5 +16,6 @@ add_projects( thread thread_pool timeout + timer wait_handle ) diff --git a/examples/xtd.core.examples/threading/README.md b/examples/xtd.core.examples/threading/README.md index d44a67606e6a..855d98e1f401 100644 --- a/examples/xtd.core.examples/threading/README.md +++ b/examples/xtd.core.examples/threading/README.md @@ -14,4 +14,5 @@ * [thread](thread/README.md) shows hows how to use [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) class. * [thread_pool](thread_pool/README.md) shows hows how to use [xtd::threading::thread_pool](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread__pool.html) class. * [timeout](timeout/README.md) shows how to use [xtd::threading:timeout](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1timeout.html) class. +* [timer](timeout/README.md) shows how to use [xtd::threading:timer](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1timer.html) class. * [wait_handle](wait_handle/README.md) shows how to use [xtd::threading:wait_handle](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1wait__handle.html) class. diff --git a/examples/xtd.core.examples/threading/timer/CMakeLists.txt b/examples/xtd.core.examples/threading/timer/CMakeLists.txt new file mode 100644 index 000000000000..4b9e9ac8d5be --- /dev/null +++ b/examples/xtd.core.examples/threading/timer/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.5) + +project(timer) +find_package(xtd REQUIRED) +add_sources(README.md src/timer.cpp) +target_type(CONSOLE_APPLICATION) diff --git a/examples/xtd.core.examples/threading/timer/README.md b/examples/xtd.core.examples/threading/timer/README.md new file mode 100644 index 000000000000..70fbca4b20ee --- /dev/null +++ b/examples/xtd.core.examples/threading/timer/README.md @@ -0,0 +1,49 @@ +# thread_pool + +Shows how to use [xtd::threading::thread_pool](https:gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread_pool.html) class. + +## Sources + +[src/thread_pool.cpp](src/thread_pool.cpp) + +[CMakeLists.txt](CMakeLists.txt) + +# Build and run + +Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following: + +```cmake +xtdc run +``` + +# Output + +``` +08:45:24.070 Creating timer. + +08:45:25.076 Checking status 1. +08:45:25.333 Checking status 2. +08:45:25.591 Checking status 3. +08:45:25.847 Checking status 4. +08:45:26.101 Checking status 5. +08:45:26.365 Checking status 6. +08:45:26.611 Checking status 7. +08:45:26.864 Checking status 8. +08:45:27.118 Checking status 9. +08:45:27.375 Checking status 10. + +Changing period to .5 seconds. + +08:45:27.885 Checking status 1. +08:45:28.389 Checking status 2. +08:45:28.897 Checking status 3. +08:45:29.402 Checking status 4. +08:45:29.913 Checking status 5. +08:45:30.414 Checking status 6. +08:45:30.922 Checking status 7. +08:45:31.428 Checking status 8. +08:45:31.934 Checking status 9. +08:45:32.445 Checking status 10. + +Destroying timer. +``` diff --git a/examples/xtd.core.examples/threading/timer/src/timer.cpp b/examples/xtd.core.examples/threading/timer/src/timer.cpp new file mode 100644 index 000000000000..d9f0c9668fa9 --- /dev/null +++ b/examples/xtd.core.examples/threading/timer/src/timer.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include + +using namespace xtd; +using namespace xtd::threading; + +namespace timer_example { + class status_checker { + private: + int invoke_count = 0; + int max_count = 0; + + public: + status_checker(int count) { + invoke_count = 0; + max_count = count; + } + + // This method is called by the timer delegate. + void check_status(std::any state_info) { + auto auto_event = as(state_info); + auto now = date_time::now(); + console::write_line("{:t}.{:D3} Checking status {,2}.", + now, now.millisecond(), + ++invoke_count); + + if (invoke_count == max_count) { + // Reset the counter and signal the waiting thread. + invoke_count = 0; + auto_event.set(); + } + } + }; + + class program { + public: + static void main() { + // Create an auto_reset_event to signal the timeout threshold in the + // timer callback has been reached. + auto auto_event = auto_reset_event {false}; + + auto status_checker = timer_example::status_checker {10}; + + // Create a timer that invokes check_status after one second, + // and every 1/4 second thereafter. + auto now = date_time::now(); + console::write_line("{:t}.{:D3} Creating timer.\n", + now, now.millisecond()); + auto state_timer = timer {{status_checker, &timer_example::status_checker::check_status}, + auto_event, 1000, 250}; + + // When auto_event signals, change the period to every half second. + auto_event.wait_one(); + state_timer.change(0, 500); + console::write_line("\nChanging period to .5 seconds.\n"); + + // When auto_event signals the second time, dispose of the timer. + auto_event.wait_one(); + state_timer.close(); + console::write_line("\nDestroying timer."); + } + }; +} + +startup_(timer_example::program); + +// This example produces output similar to the following: +// +// 08:45:24.070 Creating timer. +// +// 08:45:25.076 Checking status 1. +// 08:45:25.333 Checking status 2. +// 08:45:25.591 Checking status 3. +// 08:45:25.847 Checking status 4. +// 08:45:26.101 Checking status 5. +// 08:45:26.365 Checking status 6. +// 08:45:26.611 Checking status 7. +// 08:45:26.864 Checking status 8. +// 08:45:27.118 Checking status 9. +// 08:45:27.375 Checking status 10. +// +// Changing period to .5 seconds. +// +// 08:45:27.885 Checking status 1. +// 08:45:28.389 Checking status 2. +// 08:45:28.897 Checking status 3. +// 08:45:29.402 Checking status 4. +// 08:45:29.913 Checking status 5. +// 08:45:30.414 Checking status 6. +// 08:45:30.922 Checking status 7. +// 08:45:31.428 Checking status 8. +// 08:45:31.934 Checking status 9. +// 08:45:32.445 Checking status 10. +// +// Destroying timer.