# Service\_client\_v2.cpp timer\_callback

**URL:** <https://get-help.theconstruct.ai/t/service-client-v2-cpp-timer-callback/25991>\
**Category:** ROS2 Basics in 5 Days Humble (C++)\
**Created:** [August 26, 2023, 7:48pm UTC](https://get-help.theconstruct.ai/t/service-client-v2-cpp-timer-callback/25991 "2023-08-26T19:48:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimparker](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/j/d26b3c/32.png) [@jimparker](https://get-help.theconstruct.ai/u/jimparker)\
**Post date:** [August 26, 2023, 7:48pm UTC](https://get-help.theconstruct.ai/t/service-client-v2-cpp-timer-callback/25991/1 "2023-08-26T19:48:19Z")

</div>

In the service\_client\_v2.cpp code (the one that introduces calling a service from a spinning node), we have a timer\_callback that will get called every timer period (in the example case 1s). But that function starts by waiting\_for\_service for 1s, and if the service doesn’t become available during that period, it will either log an error if that was the cause, or log that everything is fine and we need to wait a little longer. But that is in a while loop, so that function will keep looping and giving the service a chance to become available, and logging that everything is ok every second until the service becomes available, at which point it will send the request.

All of that makes good sense to me, except this is a timer callback, so after that first second expired, the timer would queue up another identical function call to start going through the same loop. So, if the service actually takes more than 1s to become available, we will have 2 timer\_callback functions in the call\_back queue. And if it takes more than 2 seconds, we will have 3 function calls in the queue, etc.

Am I misunderstanding the function here, or wouldn’t it be better to make that wait\_for\_service to be an “if” instead of a “while”, and rely on the timer to kick off the next iteration of “wait for the service” ?

---

<div class="post-metadata">

**Author:** ![albertoezquerro](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/albertoezquerro/32/7031_2.png) [@albertoezquerro](https://get-help.theconstruct.ai/u/albertoezquerro)\
**Post date:** [August 28, 2023, 9:13am UTC](https://get-help.theconstruct.ai/t/service-client-v2-cpp-timer-callback/25991/2 "2023-08-28T09:13:34Z")

</div>

Hello @jimparker ,

You understood correctly. In this case, the service starts just fine so there is no issue. However, if for whatever reason the service takes longer to start, the scenario you presented would happen. So you could use an if statement in order to check the service status instead. However, keep in mind that you should reorganize the code in order to send the request ONLY when the service is available. I encourage you to modify the code and try it yourself.

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [September 2, 2023, 9:14am UTC](https://get-help.theconstruct.ai/t/service-client-v2-cpp-timer-callback/25991/3 "2023-09-02T09:14:12Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
