Mantid
Loading...
Searching...
No Matches
Pause.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11#include <Poco/Thread.h>
12
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using Mantid::Types::Core::DateAndTime;
16
17namespace Mantid::Algorithms {
18
19// Register the algorithm into the AlgorithmFactory
21
22
23const std::string Pause::name() const { return "Pause"; }
24
26int Pause::version() const { return 1; }
27
29const std::string Pause::category() const { return "Utility\\Development"; }
30
34 declareProperty("Duration", 1.0,
35 "How long to pause, in seconds. Default 1.\n"
36 "Enter a negative number to pause forever until cancelled.");
37}
38
42 DateAndTime startTime = DateAndTime::getCurrentTime();
43 const double duration = getProperty("Duration");
44
45 // Keep going until you get cancelled
46 while (true) {
47 bool breakOut = false;
48 try {
49 // This call throws if the user presses cancel
50 this->interruption_point();
51 } catch (CancelException &) {
52 // Break out of the lo
53 breakOut = true;
54 g_log.notice() << "User stopped the Pause.\n";
55 }
56 if (breakOut)
57 break;
58
59 // Sleep for 50 msec
60 Poco::Thread::sleep(50);
61
62 DateAndTime now = DateAndTime::getCurrentTime();
63 double seconds = DateAndTime::secondsFromDuration(now - startTime);
64
65 if (duration > 0) {
66 // Break when you've waited long enough
67 if (seconds > duration)
68 break;
69 // Report progress for non-infinite runs
70 this->progress(seconds / duration, "", duration - seconds);
71 }
72 }
73}
74
75} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
CancelException is thrown to cancel execution of the algorithm.
Definition Algorithm.h:136
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
Pause a script for a given duration.
Definition Pause.h:19
const std::string category() const override
Algorithm's category for identification.
Definition Pause.cpp:29
void init() override
Initialize the algorithm's properties.
Definition Pause.cpp:33
void exec() override
Execute the algorithm.
Definition Pause.cpp:41
int version() const override
Algorithm's version for identification.
Definition Pause.cpp:26
void notice(const std::string &msg)
Logs at notice level.
Definition Logger.cpp:126
STL namespace.