Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

Task.Run is a shorthand for Task.Factory.StartNew with specific safe arguments: Task.Factory.StartNew( action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); It was added in .Net 4.5 to help with the increasingly frequent usage of async and offloading work to the ThreadPool. Task.Factory.StartNew (added with TPL in .Net 4.0) is much more robust. You should only use it if Task.Run isn’t … Read more

How to cancel a Task in await?

Read up on Cancellation (which was introduced in .NET 4.0 and is largely unchanged since then) and the Task-Based Asynchronous Pattern, which provides guidelines on how to use CancellationToken with async methods. To summarize, you pass a CancellationToken into each method that supports cancellation, and that method must check it periodically. private async Task TryTask() … Read more

Async/await vs BackgroundWorker

This is likely TL;DR for many, but, I think comparing await with BackgroundWorker is like comparing apples and oranges and my thoughts on this follow: BackgroundWorker is meant to model a single task that you’d want to perform in the background, on a thread pool thread. async/await is a syntax for asynchronously awaiting on asynchronous … Read more

When to dispose CancellationTokenSource?

Speaking about whether it’s really necessary to call Dispose on CancellationTokenSource… I had a memory leak in my project and it turned out that CancellationTokenSource was the problem. My project has a service, that is constantly reading database and fires off different tasks, and I was passing linked cancellation tokens to my workers, so even … Read more

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

I may be missing something You are. what is the difference between doing Task.Wait and await task? You order your lunch from the waiter at the restaurant. A moment after giving your order, a friend walks in and sits down next to you and starts a conversation. Now you have two choices. You can ignore … Read more

Cancellation token in Task constructor: why?

Passing a CancellationToken into the Task constructor associates it with the task. Quoting Stephen Toub’s answer from MSDN: This has two primary benefits: If the token has cancellation requested prior to the Task starting to execute, the Task won’t execute. Rather than transitioning to Running, it’ll immediately transition to Canceled. This avoids the costs of … Read more

HttpClient – A task was cancelled?

There’s 2 likely reasons that a TaskCanceledException would be thrown: Something called Cancel() on the CancellationTokenSource associated with the cancellation token before the task completed. The request timed out, i.e. didn’t complete within the timespan you specified on HttpClient.Timeout. My guess is it was a timeout. (If it was an explicit cancellation, you probably would … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)