When to dispose CancellationTokenSource?

The class CancellationTokenSource is disposable. A quick look in Reflector proves usage of KernelEvent, a (very likely) unmanaged resource. Since CancellationTokenSource has no finalizer, if we do not dispose it, the GC won’t do it. On the other hand, if you look at the samples listed on the MSDN article Cancellation in Managed Threads, only … Read more

No ConcurrentList in .Net 4.0?

I was thrilled to see the new System.Collections.Concurrent namespace in .Net 4.0, quite nice! I’ve seen ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, ConcurrentBag and BlockingCollection. One thing that seems to be mysteriously missing is a ConcurrentList<T>. Do I have to write that myself (or get it off the web 🙂 )? Am I missing something obvious here? 12 … Read more

What’s the difference between Task.Start/Wait and Async/Await?

I may be missing something but what is the difference between doing: public void MyMethod() { Task t = Task.Factory.StartNew(DoSomethingThatTakesTime); t.Wait(); UpdateLabelToSayItsComplete(); } public async void MyMethod() { var result = Task.Factory.StartNew(DoSomethingThatTakesTime); await result; UpdateLabelToSayItsComplete(); } private void DoSomethingThatTakesTime() { Thread.Sleep(10000); } 6 Answers 6

Cancellation token in Task constructor: why?

Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing like Task.CurrentTask.CancellationToken). The token has to … Read more

HttpClient – A task was cancelled?

It works fine when have one or two tasks however throws an error “A task was cancelled” when we have more than one task listed. List<Task> allTasks = new List<Task>(); allTasks.Add(….); allTasks.Add(….); Task.WaitAll(allTasks.ToArray(), configuration.CancellationToken); private static Task<T> HttpClientSendAsync<T>(string url, object data, HttpMethod method, string contentType, CancellationToken token) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url); HttpClient … Read more

Parallel foreach with asynchronous lambda

I would like to handle a collection in parallel, but I’m having trouble implementing it and I’m therefore hoping for some help. The trouble arises if I want to call a method marked async in C#, within the lambda of the parallel loop. For example: var bag = new ConcurrentBag<object>(); Parallel.ForEach(myCollection, async item => { … Read more

When should TaskCompletionSource be used?

AFAIK, all it knows is that at some point, its SetResult or SetException method is being called to complete the Task<T> exposed through its Task property. In other words, it acts as the producer for a Task<TResult> and its completion. I saw here the example: If I need a way to execute a Func<T> asynchronously … Read more