Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EndInvoke() - optional or not?

An asynchronous question:

I've been reading over the internet LOTS of articles for and against Delegate.EndInvoke() being optional. Most of those articles are 4-5 years old. Lots of dead links.

Can anyone explain, in .NET 2.0 - is EndInvoke() indeed preventing an otherwise-inevitable memory leak, and if yes can you please specify what causes this leak?

On the same subject: If EndInvoke() is indeed a must - I find the best way to implement Fire-and-forget mechanism using a callback method that runs EndInvoke(). I'd love to hear from anyone who thinks otherwise.

Thanks, O

like image 402
tsemer Avatar asked Oct 29 '09 11:10

tsemer


People also ask

Do I have to call EndInvoke?

As a matter of practice, you should call EndInvoke because the caller may have handlers subscribed to the events that may not matter to your processing, but may matter to the consumer so as to ensure certain kinds of processing can take place at a known time/application state.

What is EndInvoke C#?

The EndInvoke method retrieves the results of the asynchronous call and releases the resource used by the thread. The parameters of EndInvoke include the out and ref parameters of the method that you want to execute asynchronously, plus a reference to the IAsyncResult returned by the BeginInvoke.


1 Answers

For Delegate.EndInvoke, you should call it. For Control.EndInvoke, the WinForms team has said that you don't need to call it. I don't know about the equivalent for WPF, but I think it's a good idea to do so unless you've got a really good reason to believe you don't have to.

I have some "fire and forget" code for delegates in my threading article - about half way down (search for "fire").

like image 177
Jon Skeet Avatar answered Sep 28 '22 09:09

Jon Skeet