What is the best practice for thread pre-emption/cancellation? I have the following scenario: (And do not yet use Parallel Extensions for .Net)
An external service call often takes a long time to complete (as little as 1 second and as high as 10+ seconds). This brings an unacceptable latency down to the UI layer (the UI is in a different application using a web service component written in .Net using COM interop.) Anyway, the base problem is a single call which has a variable latency and the code cannot be granularized further as the latency is external.
So clearly the call needs to take place asynchronously. This is now almost implemented. The problem then arises, what to do with cancellation. If the code was fine-grained it would be possible to make it regularly read a cancellation state variable. This is not the case - the call is "monolithic" - so such a variable cannot be used. Then looking at the Thread API there is a method called Abort(). Absent a brutal sounding method like Kill() (which would obviously also be evil), I expect this to be the suitable method for this scenario. Right or?
I use a "synchronization counter" to ensure the worker thread doesn't modify the environment if it accidentally completes post-cancellation.