I know there are various ways to limit how long a task runs under .Net and I wondered if there are any others I've missed or modifications/improvements to the methods I've used previously.
Where I am unclear on how exactly a methodology functions I have included questions on it.
Existing methods I'm aware of though not necessarily used myself:
- Create a
Thread
, poll for it to finish for a certain time then kill the thread. This solution is not great as it relies onThreadAbortException
's which are somewhat nasty and if I remember you have no guarantee where you'll code will exit i.e. it may leave unamanged resources in use etc. - Use the
IAsyncResult
pattern, the problem with this is that you can wait for a certain amount of time but there isn't a simple way to signal that you want the request to be aborted so you have to rely on setting a boolean flag (or similar) which is checked for inside the async code and causes it to halt. Issue here is that if the async code is stuck in a certain section of the code it may continue to run for some time before it actually terminates. - I've often seen people recommend
BackgroundWorker
for async stuff but can you use this under ASP.Net (I'd assume so) and does it have a simple way of terminating the async process after a certain time? - Use the Task API in .Net 4.0 (currently all my work is constrained to .Net 3.5 so not an option for me). From a quick read of the MSDN documentation it looks like you can easily cancel a Task using the
CancellationToken
but how quickly does a cancellation take affect and does it ensure anyfinally
blocks get called.
All solutions/suggestions/methodologies welcome