Results 1 to 7 of 7

Thread: BeginInvoke and EndInvoke...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    BeginInvoke and EndInvoke...

    Anyone have an example that uses BeginInvoke and EndInvoke and checks the invocation status in the EndInvoke? How is it done?

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    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:
    1. Class DelegateTest
    2.  
    3.     Public Delegate Sub AsyncDelegate()
    4.  
    5.     Public Sub New()
    6.         Dim RunDelegate As AsyncDelegate = AddressOf Run
    7.         RunDelegate.BeginInvoke(AddressOf Callback, RunDelegate)
    8.     End Sub
    9.  
    10.     Public Sub Callback(ByVal ar As IAsyncResult)
    11.         ar.AsyncState.EndInvoke(ar)
    12.     End Sub
    13.  
    14.     Public Sub Run()
    15.         System.Threading.Thread.Sleep(2000)
    16.     End Sub
    17.  
    18. End Class

  3. #3
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    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.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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?

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    The gory details are in: Advanced .NET archives
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    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
  •  



Click Here to Expand Forum to Full Width