/// <summary> /// Waits until the thread of this watcher has no threads to watch and terminates itself. /// Because a new watcher thread will be started again on <see cref="Watch"/>, /// this operation is only useful when you want to ensure that the watcher thread is terminated /// <strong>after</strong> your application is shut down and there's no chance of calling <see cref="Watch"/> /// afterwards. /// </summary> /// <param name="timeout"></param> /// <returns><c>true</c> if and only if the watcher thread has been terminated.</returns> public static bool AwaitInactivity(TimeSpan timeout) { Thread watcherThread = ThreadDeathWatcher.watcherThread; if (watcherThread != null) { watcherThread.Join(timeout); return(!watcherThread.IsAlive); } else { return(true); } }
/// <summary> /// Waits until the thread of this watcher has no threads to watch and terminates itself. /// Because a new watcher thread will be started again on <see cref="Watch"/>, /// this operation is only useful when you want to ensure that the watcher thread is terminated /// <strong>after</strong> your application is shut down and there's no chance of calling <see cref="Watch"/> /// afterwards. /// </summary> /// <param name="timeout"></param> /// <returns><c>true</c> if and only if the watcher thread has been terminated.</returns> public static bool AwaitInactivity(TimeSpan timeout) { Thread watcherThread = Volatile.Read(ref ThreadDeathWatcher.watcherThread); if (watcherThread is object) { _ = watcherThread.Join(timeout); return(!watcherThread.IsAlive); } else { return(true); } }