Back to "Delegate.BeginInvoke() and memory leaks..."

This is a viewer only at the moment see the article on how this works.

To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk

This is a preview from the server running through my markdig pipeline

.NET C# Imported mostlylucidcouk

Delegate.BeginInvoke() and memory leaks...

Friday, 20 February 2004

I'm getting increasingly involved in the world of Asynchrony and have been doing a bit of digging about into some of the pitfalls of multi-threading. I came across an interesting one today involving the use of BeginInvoke without a corresponding EndInvoke when using delegates...many books (including the one I'm using most right now, the excellent Programming .NET Components by Juval Lowy) mention that a good way to fire events asynchronously (i.e., not blocking) is to simple call BeginInvoke on the delegate - cool seems easy enough! Well, reading around a bit, I discovered this by Mike Woodring which contains this statement: "Starting with the 1.1 release of the .NET Framework, the SDK docs now carry a caution that mandates calling EndInvoke on delegates you've called BeginInvoke on in order to avoid potential leaks. This means you cannot simply "fire-and-forget" a call to BeginInvoke without the risk of running the risk of causing problems. " Now, I can't find the bit in the docs which says that, but another article at O'Reilly mentions the same thing... Well, based on this evidence, it would seem to be a good idea to avoid this potential leak, luckily Mike's stuff has a handily little helper class which supports the 'Fire and Forget idiom' - dead easy, now I can have lovely fire and forget calls without worrying about unintended leaks :-)

UPDATE: Found the doc reference, if you have MSDN it's here, if not the on-line version is here

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.