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(obj ect sender, System.Windows. Forms.PaintEven tArgs e)
{
if(MyBitmap != null)
{
Graphics g = e.Graphics;
g.ResetClip();
Rectangle r = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height );
g.DrawImage(MyB itmap, r, 0, 0, MyBitmap.Width, MyBitmap.Height ,
GraphicsUnit.Pi xel);
}
}
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 _replayPathTake n_Click(object sender, System.EventArg s e)
{
Thread replayPathThrea d = new Thread(new ThreadStart(Rep layPathTaken));
replayPathThrea d.Name = "ReplayPathTake n" + _count.ToString ();
replayPathThrea d.IsBackground = true;
replayPathThrea d.Start();
_count++;
}
Now, ReplayPathTaken gathers some information and then calls another method,
HighlightSelect edCells. HightlightSelec tedCells does a lot of work on
MyBitmap's pixles, similar to the following:
pixelColor = MyBitmap.GetPix el(selectedCell .Col,selectedCe ll.Row);
if(pixelColor == highlightColor)
{
MyBitmap.SetPix el
(selectedCell.C ol,selectedCell .Row
,_optionsProper ties.SolveColor );
}
else
{
MyBitmap.SetPix el
(selectedCell.C ol,selectedCell .Row
,_optionsProper ties.BacktrackC olor);
}
//Update screen with new bitmap
Invalidate(new Rectangle
(selectedCell.C ol,selectedCell .Row,Cell.CellS ize,Cell.CellSi ze));
Update();
So, the problem is that both the Form1_Paint method and
HighlightSelect edCells 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.Invalid OperationExcept ion' 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
_replayPathTake n_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(obj ect sender, System.Windows. Forms.PaintEven tArgs e)
{
if(MyBitmap != null)
{
Graphics g = e.Graphics;
g.ResetClip();
Rectangle r = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height );
g.DrawImage(MyB itmap, r, 0, 0, MyBitmap.Width, MyBitmap.Height ,
GraphicsUnit.Pi xel);
}
}
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 _replayPathTake n_Click(object sender, System.EventArg s e)
{
Thread replayPathThrea d = new Thread(new ThreadStart(Rep layPathTaken));
replayPathThrea d.Name = "ReplayPathTake n" + _count.ToString ();
replayPathThrea d.IsBackground = true;
replayPathThrea d.Start();
_count++;
}
Now, ReplayPathTaken gathers some information and then calls another method,
HighlightSelect edCells. HightlightSelec tedCells does a lot of work on
MyBitmap's pixles, similar to the following:
pixelColor = MyBitmap.GetPix el(selectedCell .Col,selectedCe ll.Row);
if(pixelColor == highlightColor)
{
MyBitmap.SetPix el
(selectedCell.C ol,selectedCell .Row
,_optionsProper ties.SolveColor );
}
else
{
MyBitmap.SetPix el
(selectedCell.C ol,selectedCell .Row
,_optionsProper ties.BacktrackC olor);
}
//Update screen with new bitmap
Invalidate(new Rectangle
(selectedCell.C ol,selectedCell .Row,Cell.CellS ize,Cell.CellSi ze));
Update();
So, the problem is that both the Form1_Paint method and
HighlightSelect edCells 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.Invalid OperationExcept ion' 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
_replayPathTake n_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
Comment