Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3991958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:40:41+00:00 2026-05-20T06:40:41+00:00

When using weak events as described here http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!373.entry in a Windows.Forms application the WeakEventManager

  • 0

When using weak events as described here http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!373.entry in a Windows.Forms application the WeakEventManager leaks WeakReference objects.
I think this is because without a WPF message loop the CleanupOperation is never executed, although ScheduleCleanup is called in WeakEventManager.ProtectedAddListener.

As a workaround I implemented a Cleanup function like so:

internal bool Cleanup()
{
    // The following is equivalent to 
    //    return this.Table.Purge(false);
    // but we need to use reflection to access the private members.

    PropertyInfo pi = typeof(WeakEventManager).GetProperty("Table", BindingFlags.Instance | BindingFlags.NonPublic);
    if (pi == null)
        return false;
    object table = pi.GetValue(this, null);
    MethodInfo mi = table.GetType().GetMethod("Purge", BindingFlags.Instance | BindingFlags.NonPublic);
    if (mi == null)
        return false;
    return (bool)mi.Invoke(table, new object[] { false });
}

and call this after every e.g. 16th call to ProtectedAddListener.

This works, but obviously I like to avoid this (ab)use of reflection.

So my questions are:

  1. is there a way to implement a cleanup function using public/protected members? WeakEventManager.Purge might be useful, but I don’t know how to use it.
  2. is there an easy way to run a WPF message loop in a Windows.Forms based application?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:40:42+00:00Added an answer on May 20, 2026 at 6:40 am

    This code builds a static function you can keep cached. It will eliminate the pain of reflection every time it is run and essentially does just what you had. Cache it somewhere and invoke it by passing in your weak event manager every time. Except for a one time hit (during the building/compile time) there is no further reflection.

        using System.Windows;
        using System.Linq.Expressions;
        using Expression = System.Linq.Expressions.Expression;
    
        static void Main(string[] args)
        {
            Func<WeakEventManager, bool> cleanUpFunction = BuildCleanUpFunction();
        }
    
        private static Func<WeakEventManager, bool> BuildCleanUpFunction()
        {
            ParameterExpression managerParameter = Expression.Parameter(
                typeof(WeakEventManager),
                "manager"
            );
            return Expression.Lambda<Func<WeakEventManager, bool>>(
                Expression.Call(
                    Expression.Property(
                        managerParameter,
                        "Table"
                    ),
                    "Purge",
                    Type.EmptyTypes,
                    Expression.Default(
                        typeof(bool)
                    )
                ),
                managerParameter
            ).Compile();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to test my application on older iOS versions. I am using weak
I recently tried building my own shared and weak pointers. Code that compiles using
I'm using a Dictionary with weak keys to test if an object has been
I am using the weak event manager in my WPF app to hook up
I'm using storyboard for an iOS application, my storyboard looks like this: http://d.pr/7yAY (droplr
I'm weak on my fundamentals here, so feel free to let me know if
Can someone give some examples of using weak references in .net projects ?
i'm a little confused about these two qualifiers... With ARC instead of using weak
When using ARC with iOS 5, a weak IBOutlet creates a zeroing reference, avoiding
Using LINQ on collections, what is the difference between the following lines of code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.