Callback: (Opposite to synchronous process)
Function that will be called when a process is done executing a specific task.Usage is usually in asynchronous logic.
Example in simple term
Synchronous Process:
Customer go to shopkeeper and order to customize a boot. Shopkeeper takes some time to customize it and rerun it back to the customer. Customer wait till the process and when customer get back the customized shoes it gifted to his neighbor. This complete process is the simple example of synchronous process as customer has to wait till the process of shoe customization not done by the shopkeeper.Asynchronous Process (Callback):
Customer go to shopkeeper and order to customize a boot. Shopkeeper told to the customer that it will takes some time to customize it. Customer said that I do not want to wait for the process. Take your time and when process for customization will be done then please handover the shoes to my neighbor, I do not want to wait for the process to accomplished. Shopkeeper said OK sure. Customer go back. Shopkeeper customized the shoe which takes some time and once done shopkeeper handover the shoes to the neighbor. This process is the example of asynchronous process as customer do not have to wait till the process was executing and this is also example of callback as once process of customization has been done, shoes was handover to the neighbor by the shopkeeper, which was to be done by customer in the synchronized process.Example:
public class CallbackDemo1
{
public delegate void DelegateSumOfNumbers(int x);
public void PrintSumOfNumbers(int num)
{
Console.WriteLine("Sum of numbers is " + num);
}
public void Demo()
{
int num = 5;
DelegateSumOfNumbers callBackSum = new DelegateSumOfNumbers(PrintSumOfNumbers);
InnerClassForSum innerClassForSum = new InnerClassForSum(num, callBackSum);
innerClassForSum.computeSum();
}
public class InnerClassForSum
{
private int number;
private DelegateSumOfNumbers _callBackSum;
public InnerClassForSum(int num, DelegateSumOfNumbers callBackSum)
{
number = num;
_callBackSum = callBackSum;
}
public void computeSum()
{
int sum = 0;
for (int i = 0; i <= number; i++)
{
sum += i;
}
_callBackSum(sum);
}
}
}
No comments:
Post a Comment
This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.