Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi there . i see some methods or delegate that their name conforms XXX+"Callback" pattern
.what does mean Callback ?
for example CustomPopupPlaceentCallback delegate.
Posted
Updated 27-Jan-14 4:54am
v2

Definition of callback on wiki[^].

In C#, delegate callback execute when they are needed.
 
Share this answer
 
imho the key aspect of what a "callback" is ... is: executable code that is written in a form where it can be passed as a parameter to some other method which can then execute the code.

It is typical to refer to the execution of the code in a callback as "invocation."

Different languages vary in how they implement this functionality: for example, a common description outside the .NET "world" would be: to pass a pointer to a function. But, .NET was designed not to make explicit use of pointers.

Keep in mind that .NET doesn't formally distinguish between the terms "function" and "method," as many other languages do.

Another frequent term in describing the operational behavior of callbacks is: deferred execution. In .NET you can think of creating Delegates as being a way to encapsulate executable code, and then "hand it off" to other methods that use the code.

I think an example may be useful:

Assume: you have two TextBoxes on a Form, 'textBox1, 'textbox2, a Button, 'SomeButton
C#
private void SomeButton_Click(object sender, EventArgs e)
{
    // define a delegate using the convenient 'Func notation
    // and using lambda notation to define the parameters
    // and function body
    // that takes two integers as parameters and returns a string
    Func<int, int, string> ReturnStringCallBack = (x, y) =>
    {
            return textBox1.Text
            + " " + x.ToString()
            + " " + textBox2.Text
            + " " + y.ToString();
    };

    // call 'executeFunc passing parameters and a pointer to the callback
    string result = executeFunc(100, 200, ReturnStringCallBack);

    // show the result of the deferred execution of ReturnStringCallBack
    MessageBox.Show(result);
}

private string executeFunc(int x, int y, Func<int, int, string> theCallBack)
{
    // pause for four seconds
    System.Threading.Thread.Sleep(4000);

    // invoke the callback using the supplied parameter values
    return theCallBack(x,y);
}
The key thing here is that you understand that you can compose a unit of executable code as needed and pass it to some external method/context for deferred, any-time, execution (invocation), and that after the unit of code's deferred execution control will return to the method/context in which the callback originated.

If your delegate (unit of executable code) returns a value, as any delegate created using the 'Func notation does, then you can use the value returned in the code that defined the delegate.

For creating a unit of code (delegate) to be passed as a parameter that does not return a value, you can use the 'Action notation.
 
Share this answer
 
v4
Comments
Rahul VB 27-Jan-14 14:28pm    
fantastic +5, very nice Sir. I hope i could myself down vote my answer.
A callback is simply something that is passed to another piece of code so that the other piece of code can do something back at the calling side. Okay, that's a lot of very confusing wording for a very simple idea. Perhaps an example will help:

  • I've got my fancy application that calculates salaries. My code calls a service on Server A that does all the processing, and I want that code to give me an updated salary value when it's finished.
    • I want this code to be kicked off and forgotten about - by that, I mean that I want to just call the service and then carry on doing other things. I don't want to wait for that code to finish processing.
  • By passing a callback to that code, it can tell me what the updated salary value is when it is ready.
  • My application now doesn't have to wait for the service to finish - I can run my app happy in the knowledge that it will fire an update back to me at some point.
 
Share this answer
 
Comments
Rahul VB 27-Jan-14 11:29am    
Very nice explanation Sir, just look at my explanation too and please make necessary changes.
I hope i have not complicated the explanation.
Thanks and regards
Pete O'Hanlon 27-Jan-14 11:45am    
I'm confused as to what changes you're expecting me to make. Perhaps you could enlighten me.
Rahul VB 27-Jan-14 12:35pm    
hello Sir, no no i meant to say, i think there was a lot of stuff missing in my explanation. Your explanation was very short and to the point and accurate. It contained a lot more than what i wrote.And i introduced delegates too, i think it wasnt required. Please do guide me.

Thanks,
Pete O'Hanlon 27-Jan-14 12:40pm    
Ah, I see. I think the biggest issue with your explanation is the bit where you say "A callback means, when an operation is over you call a method." While I can see what you are trying to say here, the explanation doesn't read quite right. What would improve this explanation would be to detail how the call is back to the calling object. Apart from that, your explanation wasn't too bad.
Rahul VB 27-Jan-14 12:44pm    
Thanks a ton Sir, yes precisely what i wanted to say. A lot of study is required on my part. I need to improve my writing skills as well as knowledge. Very correct.
Hi friend,

Quote:
Callback


A callback means, when an operation is over you call a method. Let me put forth a scenario in front of you.

Suppose you are a student, waiting for a result. You get a call from a friend saying

Quote:
Hey Ali results are out go and check your results.


Now you get a call from any of your relatives:

Quote:
Hey Ali results are out go and check the results and bring your results to me



Please notice the difference between the 2 quotes. The second one can be called a callback. You are (Pay attention here) "CALLING BACK" the function which tells you the result of operation which you just performed.

Look at the code below:


C#
public delegate void mydel();

      static void fun1()
      {
          Console.WriteLine("1st operation is done");

      }

      static void fun2()
      {
          Console.WriteLine("2nd operation is done");

      }



      static void Main(string[] args)
      {
          mydel del = new mydel(fun1);
          del += new mydel(fun2);////add a new function
          /////Perform some operation....... and when done just invoke the delegate as below, so you are using it as a callback
          del.Invoke();
          Console.ReadLine();
      }




Please note that firstly i am performing some operation, as i wrote in the comment. Just write a code snippet and if the work is done just invoke the delegate.


Brother, i have not used the delegate

Quote:
CustomPopupPlaceentCallback


But just refer:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.custompopupplacementcallback(v=vs.110).aspx[^]


If you look at the msdn blog: you will notice that its return type is
Quote:
CustomPopupPlacement[]


Which means that it returns something, so just use that delegate and if the operation is successful it will return the above data.


So you will have to read about delegates also. I hope this helps you, there are many uses of delegates, it depends upon how you use delegates in your programming.


Thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900