Skip to content

Commit 32e2c0d

Browse files
committed
Fix timeout for script sender example
1 parent d9a22c4 commit 32e2c0d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

examples/script_sender.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
//----------------------------------------------------------------------
2525

26+
#include <chrono>
27+
#include <sstream>
2628
#include <iostream>
2729
#include <thread>
2830

@@ -40,13 +42,25 @@ int main(int argc, char* argv[])
4042
}
4143
urcl::control::ScriptSender sender(PORT, "textmsg(\"Hello, World!\")");
4244

43-
std::cout << "Waiting for incoming requests on port " << PORT << "..." << std::endl;
45+
std::stringstream ss;
46+
ss << "Waiting for incoming requests on port " << PORT;
47+
if (second_to_run > 0)
48+
{
49+
ss << " for " << second_to_run << " seconds";
50+
}
51+
else
52+
{
53+
ss << " indefinitely";
54+
}
55+
56+
std::cout << ss.str() << std::endl;
4457

45-
unsigned long startTime = clock();
46-
while (second_to_run < 0 || ((clock() - startTime) / CLOCKS_PER_SEC) < static_cast<unsigned int>(second_to_run))
58+
const auto start_time = std::chrono::system_clock::now();
59+
while (second_to_run < 0 || std::chrono::system_clock::now() - start_time < std::chrono::seconds(second_to_run))
4760
{
48-
std::this_thread::sleep_for(std::chrono::seconds(1));
61+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
4962
}
63+
std::cout << "Timeout reached" << std::endl;
5064

5165
return 0;
5266
}

0 commit comments

Comments
 (0)