467,081 Members | 1,254 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Problem with System.Drawing.Graphics as a static object

Hi,

I'm getting "object is currently in use elsewhere" when I use
System.Drawing.Graphics.MesureString.

This is what I do:
My controls use a utility class the helps it to mesure strings.
To get the best performance for the utility class, its members, as well as
the System.Drawing.Graphics object, are static:

internal sealed class Utils
{
static System.Drawing.Bitmap oBmp;
static System.Drawing.Graphics oGrf;
.....
.....
}
The members are initialized in the static constructor:

static Utils()
{
oBmp = new System.Drawing.Bitmap(100, 150); //dummy numbers
oGrf = new System.Drawing.Graphics.FromImage(oBmp);
oGrf.PageUnit = System.Drawing.GraphicsUnit.Pixel;
.....
.....
}
In that way there is only one object for the controls.
Every control calls this method one time when the control is loading:

public static float CalcStringLength(string InText)
{
System.Drawing.SizeF oSizeF = oGrf.MesuereString(InText, oFontInfo);
return oSizeF.Width;
}

The problem is that when 10 users try to activate 10 controls that call to
CalcStringLength I get this error:
"the object is currently in use elsewhere"
I know this object is meant to be disposed but because it is static I have
no idea when to dispose it...

This didn't help either:
public static float CalcStringLength(string InText)
{
try
{
System.Drawing.SizeF oSizeF = oGrf.MesuereString(InText, oFontInfo);
return oSizeF.Width;
}
catch
{
// restart the graphic object
oGrf.Dispose;
oGrf = System.Drawing.Graphics.FromImage(oBmp);
return CalcStringLength(InText);
}
}
Is there any way that I can work with the graphic object as static object
without getting this error?

Thanks in advance,
Hadar.

Nov 17 '05 #1
  • viewed: 2781
Share:
1 Reply
You need to make it public rather than static, and require a separate
instance of it for each process/thread that needs it.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

"Hadar" <cr****@bezeqint.net> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Hi,

I'm getting "object is currently in use elsewhere" when I use
System.Drawing.Graphics.MesureString.

This is what I do:
My controls use a utility class the helps it to mesure strings.
To get the best performance for the utility class, its members, as well as
the System.Drawing.Graphics object, are static:

internal sealed class Utils
{
static System.Drawing.Bitmap oBmp;
static System.Drawing.Graphics oGrf;
....
....
}
The members are initialized in the static constructor:

static Utils()
{
oBmp = new System.Drawing.Bitmap(100, 150); //dummy numbers
oGrf = new System.Drawing.Graphics.FromImage(oBmp);
oGrf.PageUnit = System.Drawing.GraphicsUnit.Pixel;
....
....
}
In that way there is only one object for the controls.
Every control calls this method one time when the control is loading:

public static float CalcStringLength(string InText)
{
System.Drawing.SizeF oSizeF = oGrf.MesuereString(InText, oFontInfo);
return oSizeF.Width;
}

The problem is that when 10 users try to activate 10 controls that call to
CalcStringLength I get this error:
"the object is currently in use elsewhere"
I know this object is meant to be disposed but because it is static I have
no idea when to dispose it...

This didn't help either:
public static float CalcStringLength(string InText)
{
try
{
System.Drawing.SizeF oSizeF = oGrf.MesuereString(InText, oFontInfo); return oSizeF.Width;
}
catch
{
// restart the graphic object
oGrf.Dispose;
oGrf = System.Drawing.Graphics.FromImage(oBmp);
return CalcStringLength(InText);
}
}
Is there any way that I can work with the graphic object as static object
without getting this error?

Thanks in advance,
Hadar.

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Franco Gustavo | last post: by
3 posts views Thread by Jeroen Ceuppens | last post: by
11 posts views Thread by Sagaert Johan | last post: by
1 post views Thread by Bardo | last post: by
1 post views Thread by jty202 | last post: by
2 posts views Thread by JR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.