B
Brian Smith
My problem concerns the fact that PictureBoxes appear to retain a lock on
the file supplying the image, and I cannot work out how to release this lock
deterministically (within the Dispose method).
I have a UserControl which contains a PictureBox. On my form I have a Panel
containing a collection of these UserControls. Each has it's image loaded
using Image.FromFile(). My program needs to delete the illustrated files at
certain times, but each File.Delete() attempt causes an exception reporting
File in Use. I have proved conclusively that it is the UI PictureBox control
that is causing this, even though I clear the UI down before I attempt the
Delete.
I've written a lot of what you might assume would be unnecessary Dispose()
code to try and clean up these locks. Can anyone shed light on how this
should be done, PLEASE???
In my UserControl, I have (thus far!):
protected override void Dispose( bool disposing )
{
if ( disposing )
{
// attempt to destroy the PictureBox and all its resources
Controls.Remove(picThumb) ;
picThumb.Image = null ;
picThumb.Click -= new System.EventHandler(this.picThumb_Click);
picThumb.MouseEnter -= new
System.EventHandler(this.picThumb_MouseEnter);
picThumb.Dispose();
picThumb = null;
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
And on my Form, I run this code:
while (panel1.Controls.Count > 0)
{
Control ctl = panel1.Controls[0] ;
panel1.Controls.RemoveAt(0) ;
ctl.Dispose();
}
panel1.Controls.Clear() ;
Application.DoEvents() ;
// now attempt to Delete files...but it will fail.
Brian Smith
the file supplying the image, and I cannot work out how to release this lock
deterministically (within the Dispose method).
I have a UserControl which contains a PictureBox. On my form I have a Panel
containing a collection of these UserControls. Each has it's image loaded
using Image.FromFile(). My program needs to delete the illustrated files at
certain times, but each File.Delete() attempt causes an exception reporting
File in Use. I have proved conclusively that it is the UI PictureBox control
that is causing this, even though I clear the UI down before I attempt the
Delete.
I've written a lot of what you might assume would be unnecessary Dispose()
code to try and clean up these locks. Can anyone shed light on how this
should be done, PLEASE???
In my UserControl, I have (thus far!):
protected override void Dispose( bool disposing )
{
if ( disposing )
{
// attempt to destroy the PictureBox and all its resources
Controls.Remove(picThumb) ;
picThumb.Image = null ;
picThumb.Click -= new System.EventHandler(this.picThumb_Click);
picThumb.MouseEnter -= new
System.EventHandler(this.picThumb_MouseEnter);
picThumb.Dispose();
picThumb = null;
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
And on my Form, I run this code:
while (panel1.Controls.Count > 0)
{
Control ctl = panel1.Controls[0] ;
panel1.Controls.RemoveAt(0) ;
ctl.Dispose();
}
panel1.Controls.Clear() ;
Application.DoEvents() ;
// now attempt to Delete files...but it will fail.
Brian Smith