I started coding in C# and have never had the opportunity to use callbacks though I have used delegates for event wiring. What is the real application of callbacks. I would be grateful if you could give some link that explains about callbacks in a straight forward way without C++ prerequisites.
Share
A callback is actually a delegate, i.e. a reference to a function. Callbacks are often used in asynchronous (multi-threaded) scenarios to notify the caller when the asynchronous operation has finished: The asynchronous method gets a callback/delegate as a parameter and calls this delegate after it has finished its work, i.e. it “calls back”. Using callbacks/delegates enables the caller to decide which operation is called because he passes in the parameters.
Example:
When the user starts a long running operation by clicking on a button, you could set the mouse pointer to a WaitCursor and start the long running operation on another thread. Now, how do you know when you may reset the mouse pointer to the normal ArrowCursor? Answer: using Callbacks. You simply create a method which resets the cursor to an arrow and then pass a reference to this method (a delegate) as the callback parameter. Then this method is called when the operation finished, and your cursor is reset.
Actually, events are also some sort of callbacks: you register a delegate to be notified when a certain event occurs. When this event occurs, you are called back using the provided delegate.