Skip to main content

Parallel Programming


If you are writing multi threaded application with .Net , there are various options to choose from. There is Thread,Background worker, delegate etc. 
.Net framework 4.5 has introduced another approach towards parallel programming "async and await"With async n await, code follows a logical structure which looks similar to synchronous code and provides benefits of asynchronous programming.




Above is an example of a program which downloads the historical prices from Yahoo finance and saves it as a csv file. 

Here Act_Async is an asynchronous method which makes a call to GetStreamAsync and after that performs some operations (which are some print operation for ex.). Once those operation are complete and it requires the output of GetStreamAsync , it await on the return object (which is stream in this case). Once await is called, control is returned to the calling code (which is main method).

In Main method, once the control comes back  it performs some operation (which are again some print operation for ex.) and the thread is never blocked. when it needs the output from Act_Async method, and if Act_Async method has not come back yet, it waits for it to come back. Once the results are back, it proceed with the result for further processing .

Below is similar method written using Thread class.



Some of the key differences- 


  1.  You need to make use of a shared variable which thread updates since ThreadStart delegate can not point to a method which returns a value (difficult to use with functional programming) while async method can return an object.  
  2. The flow of the code is not as smooth as with async n await. code may look similar but code with async n await is much more easy to understand and follows a sequential structure. 
  3. The flexibility with using Thread is that You can manage it ( Do you really want to !!!) like setting up background/foreground , priority etc . 


There may be some cases where you need certain notification on progress of parallel tasks (some of these are provided in background worker class) those are not available here but you can use async n await in almost any case where you are using any other threading approach for parallel processing.

For more details , visit MSDN


Comments

Popular posts from this blog

The view or its master was not found or no view engine supports the searched locations!!!

On an MVC application, if you are getting error " The view or its master was not found or no view engine supports the searched locations " and you see that this view (.cshtml file)is already present under the correct folder structure and every thing is correct and you have also tried a lot of posts on SO on how to fix that and that hasn't helped,  just check the build action for this file and make sure that it is set to "content". I have seen the build action set to None (by mistake)and after you deploy/publish  the application, that view would be missing  and you get this error .

Top Tweets

FSharp (5th Sept 2013) when I search for F# tweets , there are so many duplicate tweets and people are having conversation with F# hashtag. I'm not really interested in conversation , but want to check what cool stuff has been shared about F#. so wrote a simple script which searches twitter for F# hashtag, removes duplicate tweets and tweets having conversation. Here is the result- New F# Cheatsheet in PDF and HTML format using FSharp.Formatting tool http://t.co/iczl1Ge3Hz #fsharp F# Snippet: Missile Command playable in tsunami.io http://t.co/BiLQxCykJa #fsharp Building a game in a day with #fsharp & #monogame slides & code samples: http://t.co/8eX64NsqOl #fsharp #mongodb provider meetup w/ @visemet starting in 15 minutes or so - you can join us online here. https://t.co/o8egTRDAn7 Awesome custom #fsharp query support for #mongodb by @visemet! http://t.co/hJQDNhyNSt #fsharp #mongodb provider meetup w/ @visemet starting in 15 minutes or so - ...

FSharp -Top tweets (10/09/2013)

(12) Posted: first steps with http://t.co/WvWRwS72oG support vector machine in #fsharp. http://t.co/s9yEA3HZFD (7) If you missed @visemet presenting the #mongodb provider for #fsharp, recording is online via @c4fsharp: https://t.co/9RKx… (6) Stanford Word Segmenter is available on NuGet for #Fsharp and #Csharp http://t.co/M8ZEKQHihq #nlp #dotNet @stanfordnlp (5) Forward pipe from #fsharp and #ocaml in #scala https://t.co/cvZkMbddGF (5) Playing with an upcoming open-source #fsharp #dataframe library and Titanic data set :-) cc @brandewinder @ptrelford http://t.co/pJfUdUG5eu (4) A few spots available for Machine Learning Hands On http://t.co/INgp7OmGki with @moloneymb tomorrow at #skillsmatter #fsharp (2) Looking for tutorials around the line of Learn F# by building ... http://t.co/xcefPaBY9x #fsharp (2) F# Weekly #36 2013 http://t.co/oSK4QX2WBx #fsharp (0) @kot_2010 @tomaspetricek You are right. Macro implementation of the #fsharp compu...