G
Guest
Hey guys,
In my app I have a bitmap where drawing is done and in the form's paint
method I show the bitmap:
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(MyBitmap != null)
{
Graphics g = e.Graphics;
g.ResetClip();
Rectangle r = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, r, 0, 0, MyBitmap.Width, MyBitmap.Height,
GraphicsUnit.Pixel);
}
}
I also have a context menu option to do some work on the bitmap
(getting/setting some pixels). When the menu item is clicked, I start the
work on a separate thread because it takes a while and doing it in on the GUI
thread woul force me to use DoEvents() in order for the form to respond to
user interactions.
private void _replayPathTaken_Click(object sender, System.EventArgs e)
{
Thread replayPathThread = new Thread(new ThreadStart(ReplayPathTaken));
replayPathThread.Name = "ReplayPathTaken" + _count.ToString();
replayPathThread.IsBackground = true;
replayPathThread.Start();
_count++;
}
Now, ReplayPathTaken gathers some information and then calls another method,
HighlightSelectedCells. HightlightSelectedCells does a lot of work on
MyBitmap's pixles, similar to the following:
pixelColor = MyBitmap.GetPixel(selectedCell.Col,selectedCell.Row);
if(pixelColor == highlightColor)
{
MyBitmap.SetPixel
(selectedCell.Col,selectedCell.Row
,_optionsProperties.SolveColor);
}
else
{
MyBitmap.SetPixel
(selectedCell.Col,selectedCell.Row
,_optionsProperties.BacktrackColor);
}
//Update screen with new bitmap
Invalidate(new Rectangle
(selectedCell.Col,selectedCell.Row,Cell.CellSize,Cell.CellSize));
Update();
So, the problem is that both the Form1_Paint method and
HighlightSelectedCells method need access to MyBitmap and sometimes they both
try to use it at the same time. That gives me the following error:
A first chance exception of type 'System.InvalidOperationException' occurred
in system.drawing.dll
Additional information: The object is currently in use elsewhere.
How can I make sure that only one thread has access to the bitmap at a time
and that my form remains responsive to user interaction (such as moving the
form or resizing it)?
Also, I would like to allow the user to click the menu item and start
_replayPathTaken_Click multiple times. This would create even more threads
that would need synchronized access to the bitmap.
If you have any questions please let me know. Any help is greatly
appreciated
-Flack
In my app I have a bitmap where drawing is done and in the form's paint
method I show the bitmap:
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(MyBitmap != null)
{
Graphics g = e.Graphics;
g.ResetClip();
Rectangle r = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, r, 0, 0, MyBitmap.Width, MyBitmap.Height,
GraphicsUnit.Pixel);
}
}
I also have a context menu option to do some work on the bitmap
(getting/setting some pixels). When the menu item is clicked, I start the
work on a separate thread because it takes a while and doing it in on the GUI
thread woul force me to use DoEvents() in order for the form to respond to
user interactions.
private void _replayPathTaken_Click(object sender, System.EventArgs e)
{
Thread replayPathThread = new Thread(new ThreadStart(ReplayPathTaken));
replayPathThread.Name = "ReplayPathTaken" + _count.ToString();
replayPathThread.IsBackground = true;
replayPathThread.Start();
_count++;
}
Now, ReplayPathTaken gathers some information and then calls another method,
HighlightSelectedCells. HightlightSelectedCells does a lot of work on
MyBitmap's pixles, similar to the following:
pixelColor = MyBitmap.GetPixel(selectedCell.Col,selectedCell.Row);
if(pixelColor == highlightColor)
{
MyBitmap.SetPixel
(selectedCell.Col,selectedCell.Row
,_optionsProperties.SolveColor);
}
else
{
MyBitmap.SetPixel
(selectedCell.Col,selectedCell.Row
,_optionsProperties.BacktrackColor);
}
//Update screen with new bitmap
Invalidate(new Rectangle
(selectedCell.Col,selectedCell.Row,Cell.CellSize,Cell.CellSize));
Update();
So, the problem is that both the Form1_Paint method and
HighlightSelectedCells method need access to MyBitmap and sometimes they both
try to use it at the same time. That gives me the following error:
A first chance exception of type 'System.InvalidOperationException' occurred
in system.drawing.dll
Additional information: The object is currently in use elsewhere.
How can I make sure that only one thread has access to the bitmap at a time
and that my form remains responsive to user interaction (such as moving the
form or resizing it)?
Also, I would like to allow the user to click the menu item and start
_replayPathTaken_Click multiple times. This would create even more threads
that would need synchronized access to the bitmap.
If you have any questions please let me know. Any help is greatly
appreciated
-Flack