views:

76

answers:

2

Hello there,

.NET 4 have an Class - Task. It's pretty interesting and I would like to start using it. For example I would like to create very simple Task based files downloader, with cancel possibility on every download. Can anyone introduce me with sample code of doing that ? I would like to have an list of running tasks and would like to have possibility to cancel any of them.

P.S. Code sample can be not functioning I just want to know how to use these things in a best way.

+1  A: 

If you want to be able to cancel one or more tasks, you need to create a CancellationTokenSource and pass in the CancellationToken of this to each Task. If all the Tasks must be cancelled using the same operation, they can share the same token. If each task can be cancelled independently of the other, you need to have separate CancellationTokens. Please check this guide for examples of how to cancel parallel tasks.

Brian Rasmussen
+1  A: 
Richard