waits for completion, times out after `wait` if not Infinite
| 216 | // waits for completion, times out after `wait` if not Infinite |
| 217 | // |
| 218 | std::optional<ProcessRunner::Results> timedWait(HANDLE handle, DWORD pid, |
| 219 | UILocker::Session* ls, |
| 220 | std::chrono::milliseconds wait, |
| 221 | std::atomic<bool>& interrupt) |
| 222 | { |
| 223 | using namespace std::chrono; |
| 224 | |
| 225 | high_resolution_clock::time_point start; |
| 226 | if (wait != Infinite) { |
| 227 | start = high_resolution_clock::now(); |
| 228 | } |
| 229 | |
| 230 | while (!interrupt) { |
| 231 | // wait for a very short while, allows for processing events below |
| 232 | const auto r = singleWait(handle, pid); |
| 233 | |
| 234 | if (r) { |
| 235 | // the process has either completed or an error was returned |
| 236 | return *r; |
| 237 | } |
| 238 | |
| 239 | // the process is still running |
| 240 | |
| 241 | // check the lock widget; the session can be null when running shortcuts |
| 242 | // with locking disabled, in which case the user cannot force unlock |
| 243 | if (ls) { |
| 244 | switch (ls->result()) { |
| 245 | case UILocker::StillLocked: { |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | case UILocker::ForceUnlocked: { |
| 250 | log::debug("waiting for {} force unlocked by user", pid); |
| 251 | return ProcessRunner::ForceUnlocked; |
| 252 | } |
| 253 | |
| 254 | case UILocker::Cancelled: { |
| 255 | log::debug("waiting for {} cancelled by user", pid); |
| 256 | return ProcessRunner::Cancelled; |
| 257 | } |
| 258 | |
| 259 | case UILocker::NoResult: // fall-through |
| 260 | default: { |
| 261 | // shouldn't happen |
| 262 | log::debug("unexpected result {} while waiting for {}", |
| 263 | static_cast<int>(ls->result()), pid); |
| 264 | |
| 265 | return ProcessRunner::Error; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (wait != Infinite) { |
| 271 | // check if enough time has elapsed |
| 272 | const auto now = high_resolution_clock::now(); |
| 273 | if (duration_cast<milliseconds>(now - start) >= wait) { |
| 274 | // if so, return an empty result |
| 275 | return {}; |
no test coverage detected