Problem with System.Drawing.Graphics as a static object

Discussion in 'ASP .Net' started by Kevin Spencer, Aug 28, 2003.

  1. 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" <> wrote in message
    news:...
    > 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.
    >
    >
    >
     
    Kevin Spencer, Aug 28, 2003
    #1
    1. Advertising

  2. Kevin Spencer

    Hadar Guest

    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.
     
    Hadar, Aug 28, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    687
    Kevin Spencer
    Jan 9, 2006
  2. byrd48
    Replies:
    0
    Views:
    3,605
    byrd48
    Jul 6, 2006
  3. tropos
    Replies:
    3
    Views:
    483
  4. Terrence Brannon
    Replies:
    4
    Views:
    304
    Terrence Brannon
    Jun 18, 2008
  5. defn noob
    Replies:
    1
    Views:
    435
    Mark Space
    Jun 28, 2008
Loading...

Share This Page