Jerry Nixon on Windows

Monday, March 24, 2008

Creating a CallBack with anonymous delegates

With a callback, you tell one method another method to call when processing is complete. It's cool.

Here's an example use: let's say you need to call Save() and when save is complete you want to call Close(). That's easy unless Save() is asynchronous. In that case, pass Close() as a callback delegate to the Save() method [something like: Save(CloseDelegate callback)]. When the save function is complete, it can call Close() for you. Easy enough.

Here's a simple console application to show you how to do it. Just create a console project and paste this into your Program.cs file. I wrote this in Visual Studio 2008. Here you go:

Hey! I'm Jerry Nixon, a Microsoft Engineer in Colorado. If you are into development, you are at the right place. I blog, I teach, and speak; should you see me out somewhere, say hi! Thanks for your visit.