470,624 Members | 2,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

Please explain the term Callback

(1.) What is the general meaning of the term 'callback'?
(2.) What does 'callback' mean, as used in the context of threading?

Many thanks,
Zach.
Nov 16 '05 #1
4 1598
I can help you with #1. Unfortunately I don't know much about threading
yet, so someone else will have to help you with #2.

A "callback" (which in C# is implemented using a delegate) is a pointer
or some other handle to a method that you pass to another object so
that the target object can "call you back" for some reason.

For example, take a look at the Image.GetThumbnailImage method. I call
this method to scale some images that I'm displaying in a picture
gallery. However, scaling an image is a CPU-intensive operation that
can take a while. For a large, complicated image, it can go away for a
few seconds (on my machine) before it comes back with the scaled image.
So what if my user gets tired of waiting and just wants to abandon the
effort? Well, I would want to throw up one of those progress bar
dialogs with a "Cancel" button on it, so that my user can say, "Enough
already. Forget it!" and I would stop producing the thumbnail.

However, I'm stuck. I can't do that because I called the
GetThumbnailImage method, so my object no longer has control of the
CPU... it's sitting on the stack somewhere waiting for
GetThumbnailImage to return so that it can continue executing my code.
No matter what I do, I can't make my code execute because my thread of
execution is busy in some code deep inside the Image class.

So, GetThumbnailImage gives me the option to supply a "pointer" to a
function _in my code_ that it promises to call from time to time. If
_my_ callback function, which I wrote and passed to GetThumbnailImage,
ever returns false, GetThumbnailImage will stop scaling the image and
return to its caller... my method. So, now I have the power to ask
GetThumbnailImage to stop working. I can write a little method that
every time it's called checks to see if the user has pressed the Cancel
button and, if the user has pressed the button, returns false.

Without that callback method, once I called GetThumbnailImage I would
be stuck waiting for it until it returned with the scaled image, no
matter what.

Nov 16 '05 #2
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
<snipped>

That is a very nice explanation,
thank you!
Zach.
Nov 16 '05 #3

"Zach" <00@00.00> wrote in message
news:c6***************************@freeler.nl...
(2.) What does 'callback' mean, as used in the context of threading?


To answer your second question, since Bruce did so well on your first,
callbacks within the context of threading are the same thing as they are in
all other cirucmstances, that is a method that is called back at some point
to provide information.
When you provide a callback in a threaded app, usually that callback is used
to notify an object or another thread(if the callback is marshaled back to
the original thread) that some processing has been completed.

..NETs async IO model uses callbacks quite frequently, for example, you can
issue an async read(which operates on a seperate thread or thread pool,
depending on the implementation) and have it call your method back when the
read has completed.
Nov 16 '05 #4

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:#4**************@TK2MSFTNGP10.phx.gbl...

"Zach" <00@00.00> wrote in message
news:c6***************************@freeler.nl...
(2.) What does 'callback' mean, as used in the context of threading?

To answer your second question, since Bruce did so well on your first,
callbacks within the context of threading are the same thing as they are

in all other cirucmstances, that is a method that is called back at some point to provide information.
When you provide a callback in a threaded app, usually that callback is used to notify an object or another thread(if the callback is marshaled back to
the original thread) that some processing has been completed.

.NETs async IO model uses callbacks quite frequently, for example, you can
issue an async read(which operates on a seperate thread or thread pool,
depending on the implementation) and have it call your method back when the read has completed.

Thank you!
Zach.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Raymond Hettinger | last post: by
15 posts views Thread by Felix Kater | last post: by
118 posts views Thread by 63q2o4i02 | last post: by
6 posts views Thread by Eric_Dexter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.