C# 5.0 Features : Async Programining
5 Ways of Doing Asyncronous programming.
- Async Vs Sync.
- Syncronous web request
- makes threads waiting for the request to return.
- This position aggrevates when the services are slow or services are timing out due to some problem in services.
- for example : lets say service is a waiter in a restaurent and request is client ordering for food then. incase of syncronous service the waiter will keep waiting while the client eats food and leave from there.
- Async Web request
- makes threads request for the service call and it gets relieved as soon as call is made to the service. so, that it can cater to other requests.
- And when the service returns the data a free thread will pick up the returned value and will return the result to the client origionally requested for it.
- for example : lets say service is a waiter in a restaurent and request is client ordering for food then. incase of asyncronous service the waiter will go to client2 while the client1 eats food and will getback once client1 needs anything else. hence same waiter is able to cater to more clients in the restaurant.
- How C# 5.0 await Async really works.
- How Sync call works in Asp.net MVC
- public void Index()
- {
- var WebClient = new WebClient()
- string[] result = WebClient.DownloadStrings();
return View("Index", new ViewStringModel { results = result });
- }
- How Async works in Asp.net MVC 3.
- In this case you need to inherit your controller from AsyncController.
- you have to split your action method into two parts.
- one is suffixed with Async and other one is suffixed with completed.
- For example
- public void IndexAsync()
- {
- var WebClient = new WebClient();
- AsyncManager.OutstandingOperations.Increment();
- WebClient.DownloadStringCompleted+=(sender,evt)
- {
- AsyncManager. Parameters["result"] = evt.Result;
- AsyncManager.OutstandingOperations.Decrement();
- }
- WebClient.DownloadStringAsync();
- }
- public ActionResult IndexCompleted(string[]
result) {
return View("Index", new ViewStringModel { results = result }); }
- How Async works in Asp.net MVC 4 with c# 5.0
- In this case you need to modify the method in thei manner
- public
async Task
- {
- var WebClient = new WebClient()
- var result = await WebClient. DownloadStringAsync();
return View("Index", new ViewStringModel { results = result });
- }
- public
async Task
- Managing task with TPL.
- Push Events --- How they work.
- Simplifying with SignalR.
Comments