Hi all, I have just finished debugging a windows application and have
solved the problem - however, I want to be sure that I understand the
problem before I move on. Before I detail the problem, this problem
requires some understanding of threading concepts.
Basically, the class contained both a PictureBox object as well as a
corresponding Image object (they both had a copy of the same picture).
The following was used to access the the Image object (I believe the
setter below will result in them both getting a copy of the same image
- but they won't both be pointing at the same image):
[CategoryAttribute("Appearance")]
[DescriptionAttribute("Image1")]
[DefaultValueAttribute(null)]
public Image Image1
{
get { return _image1; }
set
{
_image1 = value;
pictureBox1.Image = value;
}
}
_image1 is an object of type Image and pictureBox1 is an object of type
PictureBox.
Now as you have probably deduced, there is no obviously good reason to
have both an Image object contained in this class, since the two are
always in sync. Now, no problems emerged from this until we made this
application multi-threaded.
Once we made it multi-threaded I got the following error:
The object is currently in use elsewhere.
at System.Drawing.Image.get_Width()
at CustomPictureBox.ScaleImages(Single scaleFactor, Boolean
arrangeHorizontally) in c:\test\custompicturebox.cs:line 433
at CustomPictureBox.OnResize(Object sender, EventArgs e) in
c:\test\autopicturebox.cs:line 282
By simply removing the redundant Image object from the class and of
course any use of that member variable the problem went away.
Now in an attempt to get the most from the experience I have attempted
to make a small application to reproduce the problem to show other
people why this happens - but I can't get a small application to
reproduce the problem even though I understand the essence of the
problem.
I would be happy to send this small program to others to see if that
can "fix it" so that it will break in the above manner. But
essentially I'm just looking a bare bones windows application that
loads a picture, uses multi-threading and then results in a race
condition because of code similar to the above.
Thanks,
Novice