Note that there are some explanatory texts on larger screens.

plurals
  1. POc# singleton winform load/unload images
    primarykey
    data
    text
    <p>I have a winform application. The main form is singleton and it loads a lot of images. The images represents products, and every time I click on a image, I modify the stock of the product and reload the hashtable of products and the images. </p> <p>The issue is with every time I reload the hashtable of products from BBDD and reloads the images. At the end of the day, the application drains a lot of memory and I don't know when and how to unload the images in order to reload (because some times I can modify the image and I have to reload too)</p> <p>The code that reloads the hashtable and the buttons is:</p> <pre><code>Family fam = new Family(); List&lt;Family&gt; listFamilies = fam.loadFamilies(); htProducts.Clear(); foreach(Family family in listFamilies) { //reload all the products of each family if(htProducts[family.idFamily]==null) { List&lt;Product&gt; listProducts= fam.loadProductsFamily(family.idFamily); htProducts[family.idFamily] = listProducts; } Button bt = new Button(); bt.location = new Point(x,y); //The buttons are created dinamically bt.Parent = panelFamilies;//I have all the buttons inside a panel. try { if(family.urlImage !="") { bt.Text=family.Name; bt.Image = new Bitmap(family.urlImage); } } catch(Exception ex) { .... } } </code></pre> <p>When I click on a Family button it loads the products of that family but the code is more or less the same. And it loads images too.</p> <p>More ore less this is the code. The problem is that everyTime I click on a button, I load more buttons with the products and loads images. </p> <p>A lot of times I reload the singleton form and executes the previous code, and load the images again and drains the memory.</p> <p>¿When and how I have to unload the images?</p> <p>EDIT: </p> <p>I've added the following code before loading again the buttons with the images:</p> <pre><code>foreach (Control c in panel8.Controls) { if (c is Button) c.Dispose(); } GC.Collect(GC.MaxGeneration);// I think that isn't correct to use because I've read, but reduces the memory. </code></pre> <p>It will dispose all the buttons and images of the panel and reduce the memory before load the images again?</p> <p>btw sorry for my english :(</p> <p>Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload