Skip to content

90 susbscription dispose when undock the viewer parent container#114

Merged
boexler merged 4 commits intomasterfrom
90-susbscription-dispose-when-undock-the-viewer-parent-container
Oct 8, 2025
Merged

90 susbscription dispose when undock the viewer parent container#114
boexler merged 4 commits intomasterfrom
90-susbscription-dispose-when-undock-the-viewer-parent-container

Conversation

@boexler
Copy link
Copy Markdown
Owner

@boexler boexler commented Oct 8, 2025

Fix subscription disposal when undocking the viewer parent container

Overview

This PR addresses Issue #90 by implementing proper subscription lifecycle management for NLogViewer controls used in docking systems.

Problem

When NLogViewer controls are used in docking systems, moving controls between different parent windows causes subscription leaks and improper disposal. The root cause is that when undocking occurs, the Unloaded event is triggered, which automatically disposes the subscription, leaving the control unable to receive log events.

Solution

1. Added Manual Subscription Control Methods

  • StartListen(): Starts listening for log events by subscribing to the cache target
  • StopListen(): Stops listening for log events by disposing the subscription

2. Enhanced Parent Window Management

  • Improved parent window reference tracking
  • Added proper disposal handling when parent window closes
  • Enhanced FindChildByUid method with cycle detection and improved tree traversal

3. Comprehensive Documentation

Key Features

  • Automatic Management: Control automatically calls StartListen() when loaded and StopListen() when properly disposed
  • Safe to Call Multiple Times: Methods handle edge cases like design-time mode and missing parent windows
  • Docking System Support: Properly handles subscription lifecycle in complex UI scenarios
  • Memory Leak Prevention: Ensures subscriptions are properly disposed to prevent memory leaks

Usage Example

private void OnDockChanged(object sender, DockChangedEventArgs e)
{
    // Control is being undocked - MUST call StartListen() because 
    // the Unloaded event was triggered and disposed the subscription
    nLogViewer.StartListen();
}

Technical Details

  • Subscribes to CacheTarget's observable stream with 100ms buffering for performance
  • Manages parent window references for proper disposal
  • Prevents duplicate subscriptions
  • Handles design-time mode gracefully

Files Changed

  • src/NLogViewer/NLogViewer.xaml.cs - Added StartListen/StopListen methods and enhanced disposal logic
  • src/NLogViewer/Extensions/DependencyObjectExtensions.cs - Improved FindChildByUid method
  • README.md - Added comprehensive documentation

Testing

  • Verified proper subscription management in docking scenarios
  • Confirmed no memory leaks when moving controls between windows
  • Tested automatic disposal when parent window closes
  • Validated safe multiple calls to StartListen/StopListen methods

Breaking Changes

None - this is a backward-compatible enhancement.

Related Issues

Commits

  • 79fcfbb - Add StartListen/StopListen methods to fix docking system issue
  • 173dac7 - Refactor FindChildByUid method with cycle detection and improved tree traversal
  • d4e1c72 - Add documentation for parent window subscription disposal
  • bbd85ad - Add documentation for StartListen/StopListen methods and Issue Susbscription dispose when undock the viewer parent container. #90

- Add public StartListen() method to resume log subscription
- Add public StopListen() method to stop log subscription
- Add _isListening state tracking to prevent duplicate subscriptions
- Refactor _OnLoaded to use StartListen() method
- Refactor _Dispose to use StopListen() method
- Enhance pause functionality to use StopListen/StartListen for better performance
- Remove pause check from subscription callback since pause now stops subscription completely

This solves the issue where NLogViewer stops working when undocked from docking systems
like RadDocking and doesn't resume when docked again. Users can now call StartListen()
to resume logging after docking operations.
… traversal

- Updated method documentation to clarify UID-based element finding
- Implemented recursive approach with HashSet for cycle detection
- Improved search logic for both visual and logical trees
- Removed unused GetChildObjects method
- Enhanced performance and reliability of element discovery
- Added comment explaining the use case for parent window hook
- Clarifies behavior when NLogViewer is used in TabControl scenarios
- Prevents premature subscription disposal when switching TabItems
- Add comprehensive documentation for StartListen() and StopListen() methods
- Explain the root cause: Unloaded event triggers subscription disposal when undocking
- Clarify that StartListen() must be called in DockChanged event handler
- Reference Issue #90 and explain the docking system subscription management problem
- Provide practical usage examples for docking scenarios
@boexler boexler linked an issue Oct 8, 2025 that may be closed by this pull request
@boexler boexler merged commit b9310dd into master Oct 8, 2025
2 checks passed
@boexler boexler deleted the 90-susbscription-dispose-when-undock-the-viewer-parent-container branch October 16, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Susbscription dispose when undock the viewer parent container.

1 participant