Thursday, June 24, 2010

WPF Cover Flow Tutorial : Part 8

I've been told that Part 7 had memory leaks. In the comments, I link a blog article describing how to study memory leaks : you can watch the Private Bytes performance counter.

We need :
  • The performance counter
  • A callback displaying the counter value
  • A timer
using System.Diagnostics;
using System.Windows.Threading;

private readonly PerformanceCounter counter = GetCounter();
private readonly DispatcherTimer timer = new DispatcherTimer();

private void timer_Tick(object sender, EventArgs e)
{
long kb = Convert.ToInt64(counter.NextValue() / 1000);
perfLabel.Content = string.Format("{0,12} KB", kb.ToString("###.###.###"));
}
private static PerformanceCounter GetCounter()
{
var counter = new PerformanceCounter();
counter.CategoryName = "Process";
counter.CounterName = "Private Bytes";
counter.InstanceName = Process.GetCurrentProcess().ProcessName;
return counter;
}
public TestWindow()
{
...
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Start();
}

<Grid>
...
<Label Content="0 KB" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="perfLabel" VerticalAlignment="Top" Width="142" Foreground="Red" />
</Grid>
Here is the result in video.

Current implementation always stay close to 90 KB :


If we comment out Cover Destroy method:
public void Destroy()
{
//visualModel.Children.Remove(this);
}
The counter grows continuously :



Note : the ThumbnailManager does not handle empty image files. Loading such a file would throw an OutOfMemoryException.
Edit 2014-02-23 : Code has moved to github.

5 comments:

Unknown said...

Thanks man.
Just learning WPF and I'm trying to build myself a home entertainment system.
This is perfect for it.
Was thinking about using a touchscreen.
Would it be viable/possible to implement a cover drag function like the ipod touch??
Thanks again.
I've learned a lot from your tutorial.

Unknown said...

Hi. If i wanted to add a label to each cover that displays where would i assign the label in the code and how can i access the file info to fill the label?

ded' said...

Hi declan,
1. You can try to add cover/drag ipod style (I've never done this, maybe mouse events could help).
2. If you want to change covers, you need to update the Cover ctor.

Unknown said...

Hello again. Sorry for bothering you again.
When i click on a cover can I access the file info of the clicked cover.
I want to fill a list box with the relevant songs but i need to be able to have a string with the artist details so i can perform a search.
Any ideas?

ded' said...

In viewPort_MouseDown method of FlowControl class, coverList[i] is the clicked Cover. If you stored in the Cover instance all the relevant info you need, you're done.