Thomas_LCP
Thomas_LCP

Reputation: 53

New thread memory usage won't stop increase

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 ?

Upvotes: 0

Views: 389

Answers (1)

Clemens
Clemens

Reputation: 128013

The problem with increasing memory consumption in your code is that you have to delete the handle returned by GetHbitmap by calling DeleteObject, e.g. as explained here: https://stackoverflow.com/a/5827468/1136211


Besides that, in a WPF application you would implement the whole logic in a completely different way.

You would not use bitmap resources in Properties/Resources.resx but instead just add the image files to your Visual Studio project, e.g. in a project folder named "Images". Then set the Build Action of those files Resource as e.g. shown here: https://stackoverflow.com/a/12693661/1136211

On startup, load all those image resources into a list. Then start a DispatcherTimer that cyclically assigns them to the Source property of an Image element.

public partial class MainWindow : Window
{
    private readonly List<ImageSource> images = new List<ImageSource>();
    private int imageIndex;

    public MainWindow()
    {
        InitializeComponent();

        images.Add(new BitmapImage(new Uri("pack://application:,,,/Images/Image1.png")));
        images.Add(new BitmapImage(new Uri("pack://application:,,,/Images/Image2.png")));
        images.Add(new BitmapImage(new Uri("pack://application:,,,/Images/Image3.png")));

        ShowNextImage();

        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
        timer.Tick += TimerTick;
        timer.Start();
    }

    private void TimerTick(object sender, EventArgs e)
    {
        ShowNextImage();
    }

    private void ShowNextImage()
    {
        image.Source = images[imageIndex];
        imageIndex = (imageIndex + 1) % images.Count;
    }
}

Upvotes: 2

Related Questions