Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with System.Drawing.Graphics as a static object

Reply
Thread Tools

Problem with System.Drawing.Graphics as a static object

 
 
Kevin Spencer
Guest
Posts: n/a
 
      08-28-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
 
 
 
Hadar
Guest
Posts: n/a
 
      08-28-2003
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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners Problem - Static reference to non-static method William Colls Java 4 03-20-2012 06:28 PM
Problem: shared object loading runs constructor of a static object, but static linkage does not. tropos C++ 3 11-30-2005 04:54 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
static problem? Object reference not set to an instance of an object Michael Meckelein ASP .Net 3 04-14-2004 01:52 PM



Advertisments