Skip to content

Instantly share code, notes, and snippets.

@reejk
Created April 9, 2020 20:50
Show Gist options
  • Save reejk/a8a0e3b5569741b24311d23fe071452b to your computer and use it in GitHub Desktop.
Save reejk/a8a0e3b5569741b24311d23fe071452b to your computer and use it in GitHub Desktop.
WPF Bug: Cursors.None causes GC.Collect
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfCore
{
public class CursorCausesGCCollect : Window
{
private readonly LinkedList<Guid> _largeMemoryUsage = new LinkedList<Guid>();
private readonly Line _line;
public CursorCausesGCCollect()
{
for (int count = 1024 * 1024 * 32; count != 0; count--)
_largeMemoryUsage.AddLast(Guid.Empty);
Width = Height = 200;
ResizeMode = ResizeMode.NoResize;
_line = new Line { StrokeThickness = 2, Stroke = Brushes.Black };
Content = new Canvas
{
IsHitTestVisible = false,
Children =
{
new TextBlock
{
Text = "1. Press mouse button\n2. Move mouse quickly",
Foreground = Brushes.Gray,
Margin = new Thickness(20)
},
_line
}
};
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (CaptureMouse())
{
var pos = e.GetPosition(this);
_line.X1 = _line.X2 = pos.X;
_line.Y1 = _line.Y2 = pos.Y;
Cursor = Cursors.None;
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
ReleaseMouseCapture();
}
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
Cursor = null;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (IsMouseCaptured)
{
var pos = e.GetPosition(this);
_line.X2 = pos.X;
_line.Y2 = pos.Y;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment