Anyone have an example that uses BeginInvoke and EndInvoke and checks the invocation status in the EndInvoke? How is it done?
Printable View
Anyone have an example that uses BeginInvoke and EndInvoke and checks the invocation status in the EndInvoke? How is it done?
From the GotDotNet forums:
Quote:
To make an asynchronous delegate call in vb, you need to:
1) Declare a delegate function with same signature as the procedure
2) Create a procedure delegate instance from a procedure using the AddressOf operator
3) Call BeginInvoke on the delegate, passing in a callback function
Here's an example that asynchronously calls the Run method when the class DelegateTest is created
VB Code:
Class DelegateTest Public Delegate Sub AsyncDelegate() Public Sub New() Dim RunDelegate As AsyncDelegate = AddressOf Run RunDelegate.BeginInvoke(AddressOf Callback, RunDelegate) End Sub Public Sub Callback(ByVal ar As IAsyncResult) ar.AsyncState.EndInvoke(ar) End Sub Public Sub Run() System.Threading.Thread.Sleep(2000) End Sub End Class
The .NET documentation would seem to indicate that if you want to invoke an event asynchronously in "fire and forget" mode you can do this by calling the .BeginInvoke method and not passing any callback parameter to it. However the newsgroup evidence and our own testing have found that there is a memory leak if you do this. Therefore we would recommend you pass a delegate that calls the event's EndInvoke method even if you have no particular processing to perform when the event returns.
This has been implemented in the latest version of the PrinterQueueWatch .NET component and I suggest you also look through any calls you make to BeginInvoke to ensure they have a corresponding EndInvoke to avoid this problem.
This is good to know, especially since I fire and forgot in some of my code. How was it discovered that this causes a memory leak?
The gory details are in: Advanced .NET archives
Thanks.
How does one find memory leaks anymore with the way the GC works? I'm just curious. I mean how do we know the memory just hasn't been reclaimed? Or is there any links to testing for such occurrance?
The "Application Compatibility Test" which is included on the Windows XP CD (in \Tools) can check for resource leaks (GDI handles etc) but not memory leaks AFAIK.