I recently discover how to do task in background and try to use these in WPF for a test.
What I try to test is to create a picture carousel in a picture box.
To do this I read this, this and this and that's what I have :
public partial class Page2 : Page
{
public Thread backgroundcaroussel;
public Page2()
{
InitializeComponent();
backgroundcaroussel = new Thread(ImgFlip);
backgroundcaroussel.IsBackground = true;
backgroundcaroussel.Start();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
backgroundcaroussel.Abort();
MainWindow.Fenetre.Content = MainWindow.pgUn;
}
private void ImgFlip()
{
Again:
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate ()
{
BitmapSource btmSrc1 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_1.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc1;
});
Thread.Sleep(2000);
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,(ThreadStart)delegate ()
{
BitmapSource btmSrc2 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_2.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc2;
});
Thread.Sleep(2000);
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate ()
{
BitmapSource btmSrc3 = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources._1080p_3.GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
img_moins.Source = btmSrc3;
});
Thread.Sleep(2000);
goto Again;
}
}
When I use this code, the memory usage won't stop increase and reach 1 or 2 Go (before I stop it). I don't think that's normal :)
I also read this,this and this to fix the problem but don't clearly know what to do.
How to solve this memory consumption ? Do I use the right methodology ?