I need to stop the execution of a method if it has not completed within a limited period of time.
To do this work I can use the Thread.Abort method in this way:
void RunWithTimeout(ThreadStart entryPoint, int timeout)
{
var thread = new Thread(() =>
{
try
{
entryPoint();
}
catch (ThreadAbortException)
{ }
}) { IsBackground = true };
thread.Start();
if (!thread.Join(timeout))
thread.Abort();
}
Given that I’m using .NET 3.5, is there a better way?
Edit: following the comments here my entryPoint, but I’m looking for a good way for any entryPoint.
void entryPoint()
{
// I can't use ReceiveTimeout property
// there is not a ReceiveTimeout for the Compact Framework
socket.Receive(...);
}
Answer depends on ‘the work’. If work is something that can be safely stopped (i.e. not some I/O blocking operation) – use
Backgroundworker.CancelAsync(...)If you do have to cut hard – I’d consider using a
Process, in which case theAbortingprocess is cleaner – andprocess.WaitForExit(timeout)is your friend.Suggested TPL is great but unfortunately does not exist in .Net 3.5.
EDIT: You can use Reactive Extensions to follow Jan de Vaan’s suggestion.
Here is my ‘action timeout’ snip – it’s mainly here for others to comment on:
EDIT: Apparently this isn’t exactly what OP wanted. Here’s my attempt to devise a ‘cancelable’ socket receiver: