/// <summary> /// Cancels the task scheduled via <see cref="Watch"/>. /// </summary> public static void Unwatch(POPThread thread, Action task) { Contract.Requires(thread != null); Contract.Requires(task != null); Schedule(thread, task, false); }
/// <summary> /// Schedules the specified <see cref="Action"/> to run when the specified <see cref="Thread"/> dies. /// </summary> public static void Watch(POPThread thread, Action task) { Contract.Requires(thread != null); Contract.Requires(task != null); Contract.Requires(thread.IsAlive); Schedule(thread, task, true); }
private void CreateLongRunningTask(XParameterizedThreadStart threadStartFunc) { this.task = Task.Factory.StartNew(delegate { this.readyToStart.WaitOne(); currentThread = this; threadStartFunc(this.startupParameter); this.completed.Set(); }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
static void Schedule(POPThread thread, Action task, bool isWatch) { PendingEntries.Enqueue(new Entry(thread, task, isWatch)); if (Interlocked.CompareExchange(ref started, 1, 0) == 0) { var watcherThread = new POPThread(s => ((IRunnable)s).Run()); watcherThread.Start(watcher); ThreadDeathWatcher.watcherThread = watcherThread; } }
/// <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) { POPThread watcherThread = ThreadDeathWatcher.watcherThread; if (watcherThread != null) { watcherThread.Join(timeout); return(!watcherThread.IsAlive); } else { return(true); } }
public Entry(POPThread thread, Action task, bool isWatch) { this.Thread = thread; this.Task = task; this.IsWatch = isWatch; }