Skip to content

Commit

Permalink
Add timer example
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Aug 15, 2023
1 parent 6a069be commit 26b1088
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/xtd.core.examples/threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ add_projects(
thread
thread_pool
timeout
timer
wait_handle
)
1 change: 1 addition & 0 deletions examples/xtd.core.examples/threading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions examples/xtd.core.examples/threading/timer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions examples/xtd.core.examples/threading/timer/README.md
Original file line number Diff line number Diff line change
@@ -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.
```
99 changes: 99 additions & 0 deletions examples/xtd.core.examples/threading/timer/src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <xtd/threading/auto_reset_event>
#include <xtd/threading/timer>
#include <xtd/console>
#include <xtd/date_time>
#include <xtd/random>
#include <xtd/startup>

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<auto_reset_event>(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.

0 comments on commit 26b1088

Please sign in to comment.