Object is currently in use elsewhere.

A

aaron.radich

I'm getting an "Object is currently in use elsewhere." exception when
I try to work with a bitmap in a background thread. The bitmap gets
manipulated and then passed to the main UI thread via a delegate. Any
idea what I can do to get around this issue? The code and exception
is below. The line that generates the error is commented:

Aaron

Exception:

System.InvalidOperationException was unhandled by user code
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image._GetColorPalette()
at System.Drawing.Image.get_Palette()
at KaraokePlayer.frmMain.PlayFile(Object sender,
DoWorkEventArgs e) in C:\SongbookMaster\Win32\KaraokePlayer
\Form1.cs:line 126
at System.ComponentModel.BackgroundWorker.OnDoWork
(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart
(Object argument)
InnerException:


Code:

private void PlayFile(object sender, DoWorkEventArgs e)
{
string sCDGFilename = (string)e.Argument;
int iPaintCount = 0;
using (BinaryReader binReader = new BinaryReader(File.Open
(sCDGFilename, FileMode.Open)))
{
try
{
//Bitmap bmpBuffer = new Bitmap(iSCREEN_WIDTH,
iSCREEN_HEIGHT, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

System.Drawing.Bitmap bmpBuffer = new Bitmap(@"C:
\SongbookMaster\Win32\KaraokePlayer\bin\Debug\Background.bmp");

// If the file is not empty,
// read the application settings.
// Read 4 bytes into a buffer to
// determine if the file is empty.
byte[] barray = new byte[24];
//SUBCODE recRead;
MemoryStream ms;
BinaryReader br;
int iTickCount = 0;
int iCount = binReader.Read(barray, 0, iREAD_SIZE);
while (iCount == iREAD_SIZE)
{
ms = new MemoryStream(barray);
br = new BinaryReader(ms);
SUBCODE recBlock;
recBlock.Command = br.ReadByte();
recBlock.Instruction = br.ReadByte();
recBlock.ParityQ = br.ReadBytes(2);
recBlock.Data = br.ReadBytes(16);
recBlock.ParityP = br.ReadBytes(4);

if ((recBlock.Command & CDG.SC_MASK) ==
CDG.SC_CDG_COMMAND)
{
// the following line is getting an "Object is
currently in use elsewhere." error msg
CDG objCDG = new CDG(bmpBuffer.Palette);
objCDG.CDG_Handler(recBlock);

bmpBuffer = CopyDataToBitmap(ref
objCDG.CDG_ScreenBuffer, objCDG.Palette);
//bmpBuffer.Palette = objCDG.Palette;

// for testing purposes, lets save the bitmap to
file
if (iPaintCount % 500 == 0)
{
string sBMFilename = @"C:\Temp\Bitmaps
\BitmapImage" + iPaintCount.ToString() + ".bmp";
if (File.Exists(sBMFilename))
File.Decrypt(sBMFilename);
bmpBuffer.Save(sBMFilename);
}

// draw the bitmap onto the form
// pass the bitmap to the main ui thread
if (this.InvokeRequired)
{
NewBitmapEventArgs args = new
NewBitmapEventArgs(bmpBuffer);
this.BeginInvoke(new NewBitmapEventHandler
(UpdateBitmap), new object[] { this, args });
}
else
{
bmpMaster = bmpBuffer;
this.Invalidate();
}

// force the form to repaint itself
//this.Invalidate();
//Refresh();
bw.ReportProgress(0);

// keep track of how many times we've painted on
the form
iPaintCount++;
}


// pause for a while
iTickCount = System.Environment.TickCount;
while ((System.Environment.TickCount - iTickCount) <
1) ;

// read again
iCount = binReader.Read(barray, 0, iREAD_SIZE);

//if (iPaintCount > 1000)
// break;
}
br = null;
ms = null;
bmpBuffer = null;
//MessageBox.Show("All Done With The CDG");
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch (EndOfStreamException ex)
{
Console.WriteLine("{0} caught and ignored. " +
"Using default values.", ex.GetType().Name);
}
finally
{
binReader.Close();
}
}
}
 
B

Ben Voigt [C++ MVP]

I'm getting an "Object is currently in use elsewhere." exception when
I try to work with a bitmap in a background thread. The bitmap gets
manipulated and then passed to the main UI thread via a delegate. Any

That's not really accurate.

In reality, the bitmap gets manipulated and then passed to the main UI
thread via a delegate _and then manipulated some more_. It's that last bit,
that you left out, that causes the problem.
idea what I can do to get around this issue? The code and exception

Create a new Bitmap instance every time through the loop and your exception
will go away.
 
D

DC

I'm getting an "Object is currently in use elsewhere." exception when
I try to work with a bitmap in a background thread. The bitmap gets
manipulated and then passed to the main UI thread via a delegate. Any
idea what I can do to get around this issue? The code and exception
is below. The line that generates the error is commented:

Aaron

Exception:

System.InvalidOperationException was unhandled by user code
Message="Object is currently in use elsewhere."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image._GetColorPalette()
at System.Drawing.Image.get_Palette()
at KaraokePlayer.frmMain.PlayFile(Object sender,
DoWorkEventArgs e) in C:\SongbookMaster\Win32\KaraokePlayer
\Form1.cs:line 126
at System.ComponentModel.BackgroundWorker.OnDoWork
(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart
(Object argument)
InnerException:


Code:

private void PlayFile(object sender, DoWorkEventArgs e)
{
string sCDGFilename = (string)e.Argument;
int iPaintCount = 0;
using (BinaryReader binReader = new BinaryReader(File.Open
(sCDGFilename, FileMode.Open)))
{
try
{
//Bitmap bmpBuffer = new Bitmap(iSCREEN_WIDTH,
iSCREEN_HEIGHT, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

System.Drawing.Bitmap bmpBuffer = new Bitmap(@"C:
\SongbookMaster\Win32\KaraokePlayer\bin\Debug\Background.bmp");

// If the file is not empty,
// read the application settings.
// Read 4 bytes into a buffer to
// determine if the file is empty.
byte[] barray = new byte[24];
//SUBCODE recRead;
MemoryStream ms;
BinaryReader br;
int iTickCount = 0;
int iCount = binReader.Read(barray, 0, iREAD_SIZE);
while (iCount == iREAD_SIZE)
{
ms = new MemoryStream(barray);
br = new BinaryReader(ms);
SUBCODE recBlock;
recBlock.Command = br.ReadByte();
recBlock.Instruction = br.ReadByte();
recBlock.ParityQ = br.ReadBytes(2);
recBlock.Data = br.ReadBytes(16);
recBlock.ParityP = br.ReadBytes(4);

if ((recBlock.Command & CDG.SC_MASK) ==
CDG.SC_CDG_COMMAND)
{
// the following line is getting an "Object is
currently in use elsewhere." error msg
CDG objCDG = new CDG(bmpBuffer.Palette);
objCDG.CDG_Handler(recBlock);

bmpBuffer = CopyDataToBitmap(ref
objCDG.CDG_ScreenBuffer, objCDG.Palette);
//bmpBuffer.Palette = objCDG.Palette;

// for testing purposes, lets save the bitmap to
file
if (iPaintCount % 500 == 0)
{
string sBMFilename = @"C:\Temp\Bitmaps
\BitmapImage" + iPaintCount.ToString() + ".bmp";
if (File.Exists(sBMFilename))
File.Decrypt(sBMFilename);
bmpBuffer.Save(sBMFilename);
}

// draw the bitmap onto the form
// pass the bitmap to the main ui thread
if (this.InvokeRequired)
{
NewBitmapEventArgs args = new
NewBitmapEventArgs(bmpBuffer);
this.BeginInvoke(new NewBitmapEventHandler
(UpdateBitmap), new object[] { this, args });
}
else
{
bmpMaster = bmpBuffer;
this.Invalidate();
}

// force the form to repaint itself
//this.Invalidate();
//Refresh();
bw.ReportProgress(0);

// keep track of how many times we've painted on
the form
iPaintCount++;
}


// pause for a while
iTickCount = System.Environment.TickCount;
while ((System.Environment.TickCount - iTickCount) <
1) ;

// read again
iCount = binReader.Read(barray, 0, iREAD_SIZE);

//if (iPaintCount > 1000)
// break;
}
br = null;
ms = null;
bmpBuffer = null;
//MessageBox.Show("All Done With The CDG");
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch (EndOfStreamException ex)
{
Console.WriteLine("{0} caught and ignored. " +
"Using default values.", ex.GetType().Name);
}
finally
{
binReader.Close();
}
}
}

Hi Aaron
Im doing a cdg player but Im really confused. can u give me some help? thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top