Skip to main content

Posts

Showing posts with the label Task

When to use void and Task as return type for Async method

As most of us are aware that there can be 3 return types of the method marked as Async. Task Task<T> void When any Async method has return type as void, it means not to return anything. In case of synchronous, this is fine. But situations become difficult when it comes to asynchronous operations. While dealing with asynchronous operations, void behaves like ‘fire and forget’. It means whatever happens, but do not let caller know anything. So, it means, caller will not at all aware about the operation stage, whether it is successful or unsuccessful. As data type is void, method will not even propagate exceptions, which can be very harmful in some cases. So, always use the word void with caution. If you don’t want your Async method to return anything, then also one should prefer to use return type as Task so that at least exceptions will be propagated to the caller.

Controlling Degree of Concurrent Execution in Parallel Loops

Having multi-core machine, one can get the benefit of Parallelism for long-running and blocking tasks. I hope most of you might have used Parallel. For, Parallel. Foreach and TPL several times. Generally parallelism is used to get the benefits of all the available cores on the machine that commonly leads to the shortest execution time for any task. Did you ever thought about what can be the possible demerits of using all the available cores for just a single application? Is it really required to use all the available cores of your machine for a single application? Can't we use only a few of the cores? Is there any way to restrict these parallel loops in terms of cores? The answer of all the preceding questions is YES . But why should we bother about how many cores are participating in execution? Well, there can be several reasons behind this.  The foremost reason  that I feel is, when a single time consuming application/task is running on all the available cores utili...