views:

289

answers:

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?