-6

How Async await works in .net 4.5 and above ? how does it differ from BeginInvoke and EndInvoke

TsunamiCoder
  • 120
  • 1
  • 12
  • 3
    You can find many answers to this around the web and this is not a specific question for stack overflow. Watch this to learn async/await: http://stackoverflow.com/documentation/c%23/48/async-await#t=201701250624127863526 and take a look here for asking the good questions: http://stackoverflow.com/help/how-to-ask – Sebi Jan 25 '17 at 06:26
  • Can you explain with some diagrams the threading concepts in Async/await – TsunamiCoder Jan 25 '17 at 06:38
  • @TsunamiCoder You may find my [async intro](http://blog.stephencleary.com/2012/02/async-and-await.html) helpful. – Stephen Cleary Jan 25 '17 at 13:11

1 Answers1

0

Async and await works with the Task Library. If you write a method and want to make it async you just have to mark it as async and call await on any task inside of your method. Just the await keyword make your method async and just this code runs async. For example:

//This Method isn't async because there is no await
private async Task DoSomething()
{
   //Some work
}

//This method is async because it awaits sth.
private async Task DoSomething()
{
   await SomeOtherStuff();
}

Note that async methods return Task or Task which encapsulate your return type. This Task allows other methods to await your method. Up to this way you build a chain which ends in your GUI. So you GUI isn't blocking and responsive.

I found this diagram after 1 sec of google which describe this behaviour pretty well:

enter image description here

This has not much to do with BeginInvoke and EndInvoke, because Invoke calls are just for using Gui objects in different threads. If possible you should avoid BeginInvoke and EndInvoke and use the GUI only on your Mainthread.

Sebi
  • 3,879
  • 2
  • 35
  • 62