I've just written some code to perform a timeout action if an asynchronous task takes too long to process, but what is not clear to me is if and when the timeout instance will ever be disposed of (I think it will in the case where the asynchronous task completes in a timely fashion, but otherwise I've got no idea), or if I'm going to be accumulating instances every time I call this code.
//StartNew creates a new instance of System.Timers.Timer, and
// invokes the ActionOnTimeout after 2000ms, unless calling code
// calls "Stop" first
var timeout = ProcessTimeout.StartNew(() => ActionOnTimeout(), 2000);
//DoAsyncTask creates a new thread, does potentially slow stuff,
/// then invokes this callback
DoAsyncTask(() =>
{
if(timeout.Running)
{
timeout.Stop();
DoCallbackStuff();
}
});
(If it's any help, the ProcessTimeout class uses a System.Timers.Timer
)