Posts

Showing posts with the label Task

Gluing things together

Gluing things together One of the most important things in coding is how to hold it together. In this short article I will share some thoughts on callbacks, events, observables, promises and tasks. This is what I have experienced in my time programming and I would be glad if you could share if you don't agree with me on something. Why callbacks are bad When I started programming some years ago I started with C# and Javascript. While .NET already had the Event in Javascript callbacks were everywhere. Then the word "callback hell" was coined. In short callbacks and also Events for that matter are bad because they defere the code execution to some other place which can be hard to find especially when you are following from one callback to the next for multiple steps. Therefore it pollutes the code and can separate concern which should not be separated. Why callbacks can be good Defered execution is not always bad. To create modulation and structure callbacks an...

Components of Tasks & Threading in .NET

Image
I recommend understanding the basics of async and await before you read this article. This is a good introduction from Stephen Cleary. As you might know simply using async and await doesn't mean your code is executed on different threads. To achieve this you must use Task.Run , TaskFactory.StartNew or the Task Parallel Library . This is an introduction in the different components at play when you want to schedule tasks on different threads. TaskFactory The TaskFactory is a simple container where you can configure options for the creation and continuation of Tasks. Furthermore you can specify an canncelation Token which will be used by all Tasks created through this factory. And lastly you can define your own TaskScheduler. It's your choise if you want to use a TaskFactory to start a new task or if you want to start a task directly trough Task.Run. MSDN TaskFactory TaskScheduler The TaskScheduler is responsible to run the next task on the correct ...