Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Abort and alternatives

This is more out of personal curiosity/interest than a specific problem I'm trying to solve.

Suppose you have a program that is performing some operation on user-supplied information (such as a search string) that changes as the user types it. Suppose that you want to show the user the most relevant information for what they've typed at any given time.

If threads were really abortable, we could simply have one thread running based on the last-changed search string, and cancel any previous threads that were in progress.

Now the generally accepted best practice for doing this today is to use a delay timer as the user types, which waits for .5 to 1 second before initiating the operation. I hope it's obvious enough that this isn't an ideal solution, theoretically speaking (any kind of artificial delay imposes an artificial bottleneck that can never be overcome, even if it is only 0.5 seconds).

Furthermore, today's best practice goes on to state that any subsequent operations should wait for the previous one to complete before executing. In a world where we can't abort operations, this makes sense, but again, theoretically speaking this is far from ideal. Imagine the user types a single character and pauses just long enough for the operation to begin. Suppose that this operation takes 10 seconds to execute. The user is now forced to wait an unacceptable amount of time before the results of his/her query are visible.

An (unideal) workaround to this would be to have multiple operations executing concurrently, presuming it is safe to do so, but this would still result in significantly reduced performance.

So I'm just wondering people's thoughts on this, specific to .NET at least, and whether there are any new developments in this area since I last researched it that I should know about (parallel libraries perhaps?). I'd also be curious to know if any other languages/frameworks can handle this sort of fine-grained control of operations better than .NET can.

Cheers.

like image 214
devios1 Avatar asked Dec 28 '22 09:12

devios1


1 Answers

You should have a look at the Task Parallel Library in .NET 4 and the new cancellation model. Tasks can be run concurrently and it is safe to cancel them. It looks like a good fit for what you need.

like image 71
Mark Byers Avatar answered Dec 31 '22 10:12

Mark Byers