|
-
Nov 11th, 2003, 04:52 AM
#1
BeginInvoke and EndInvoke...
Anyone have an example that uses BeginInvoke and EndInvoke and checks the invocation status in the EndInvoke? How is it done?
-
Nov 11th, 2003, 08:33 AM
#2
From the GotDotNet forums:
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
-
Nov 18th, 2003, 04:13 PM
#3
Frenzied Member
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.
-
Nov 18th, 2003, 04:29 PM
#4
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?
-
Nov 18th, 2003, 04:34 PM
#5
Frenzied Member
The gory details are in: Advanced .NET archives
-
Nov 18th, 2003, 04:39 PM
#6
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?
-
Aug 24th, 2004, 11:00 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|