what's the callback

  • Thread starter Thread starter Alien
  • Start date Start date
A

Alien

can some please what's callback mean? and how to use that?
some one said callback is another name for event. it that true?
cheers
 
An event is a type of callback yes. A callback in general is a function
that you pass to another function as a delegate. The second will call the
first by itself to achieve some goal.

-Chris
 
hi hi
thanks for the answer. but can you give me some good and easy to understand
examples about how to use callback?

cheers
 
Callbacks are commonly used for asynchronous operations. For example,
if you make an asynchronous call to a WebService method, (using
Begin<MethodName>), it takes a callback as a parameter, so that your
main thread can continue processing, and when the web method completes,
your callback will run.
 
Callbacks by nature aren't simple. Here's a contrived sample that uses one,
but it's not very useful. Typically they're helpful in multithreaded
situations or where you're using polymorphism and/or inheritance. I'm not
going to slap a sample of that together, as it would take a while, and if
you don't understand what a callback is, you're probably not going to
understand the sample without research. I recommend you do some Googling on
delegates to see how they work and what they're used for. You'll find a
plethora of useful stuff out there.

Anyway, here's a quick, useless example:

delegate void CallbackDelegate(int val);

void Method()
{
DoWork();
OtherMethod(new CallbackDelegate(CallbackProc));
DoMoreWork();
}

void CallbackProc(int val)
{
MessageBox.Show("Callback " + val);
}

void OtherMethod(CallbackDelegate callback)
{
DoStuff();
for(int i = 0 ; i < 5 ; i++)
{
callback(i);
}
DoMoreStuff();
}

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
Back
Top