Class Watchdog
Provides a way to abort a thread after a specified timeout.
public class Watchdog : IDisposable
- Inheritance
-
Watchdog
- Implements
- Inherited Members
Remarks
When the timeout is reached, the WatchedThread is aborted by calling Abort(object) on the thread. If you specify a state object in the Watchdog(object, Duration) constructor, that object is set to the ExceptionState of the ThreadAbortException that is raised when the WatchedThread is aborted due to the timeout being elapsed.
Note that an asynchronous abort of the watched thread is done not before 150% of the specified timeout are reached. Call the CheckAbortSynchronous() method from the watched thread to abort the watched thread synchronously when the timeout is reached, but before the asynchronous abort happens.
Constructors
Watchdog(object, Duration)
Constructs a new instance.
public Watchdog(object state, Duration timeout)
Parameters
stateobjectSpecifies the value of the ExceptionState property of the ThreadAbortException that is raised when the WatchedThread is aborted.
timeoutDurationSpecifies the timeout after which the WatchedThread is aborted.
Remarks
The CurrentThread will be watched.
Watchdog(object, Duration, Thread)
Constructs a new instance.
public Watchdog(object state, Duration timeout, Thread watchedThread)
Parameters
stateobjectSpecifies the value of the ExceptionState property of the ThreadAbortException that is raised when the WatchedThread is aborted.
timeoutDurationSpecifies the timeout after which the WatchedThread is aborted.
watchedThreadThreadThe watched thread.
Properties
WatchedThread
The Thread that is watched and aborted if the timeout is elapsed.
public Thread WatchedThread { get; }
Property Value
Methods
CheckAbortSynchronous()
Can be called by the watched thread (and the watched thread only) to force the watchdog to check the timeout and consequently abort synchronously.
public void CheckAbortSynchronous()
Exceptions
- InvalidOperationException
If this method is not executed by the WatchedThread.
Dispose()
Releases all resources held by this instance.
public void Dispose()
Remarks
Note: Disposing the instance before the timeout has expired won't abort the watched thread.
~Watchdog()
When the instance is finalized, frees all resources of the instance.
protected ~Watchdog()