
We saw the Asynchronous Programming Model (APM) which fires the method(s) inside a delegate using BeginInvoke and EndInvoke. BeginInvoke executes a single method inside a delegate asynchronously. if we desire to retrieve the result of the delegate (whenever the delegate points to a function which returns a value) we would use the EndInvoke. we could also use the EndInvoke in order to wait for the delegate function to be completed (like wait, or join). in order to use the EndInvoke mechanism we require the IAsyncResult. the IAsyncResut is an object which looks a bit like the Task object. it can help us to check whether the delegate has completed or not. whenever we call the EndInvoke we pass the IAsyncResult as a parameter. Another strategy could be to assign a CallBackMethod which will be executed after the BeginInvoke delegate has been completed (without blocking our code). the CallBackMethod we write accepts the IAsyncResult as a parameter (which is automatically sent be the .NET framework [dependency injection]) so we could run the EndInvoke inside the CallBackMethod, or just check the state of the delegate. we saw that if we want to run all of the methods inside a delegate asynchronously we could write a foreach loop which iterates over the delegate methods using the GetInvocationList and fires them using the BeginInvoke. we also saw that we could pass any type of argument to the BeginInvoke method which will be passed to the IAsyncResult.AsyncState property
More topics covered:
- Delegate class. the base of MultiCastDelegate class
- DynamicInvoke
- Task implements IAsyncResult
- BeginInvoke runs on a background thread
- BeginInvoke could be executed only on a single delegate method
- EndInvoke could be executed on a specific delegate only once
- @object
Links:
- Class code: delegate
- Class code: Async-Await
- Youtube lecture code
- BeginInvoke EndInvoke – MSDN link
- MultiCastDelegate – MSDN link
- AsyncCallback method – MSDN link
- Home Work!
- Lesson Summary Video!
