472,796 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

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
1 3125
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Franco Gustavo | last post by:
Hi, How can I execute 2 GDI steps without update the screen and update them togheter?. Basically I have a form and I want to change the Form Region and change the Form Location, later show...
3
by: Jeroen Ceuppens | last post by:
Hi, When i do this in my programma, in the contstructor of the form: g.DrawImage(i,new Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel); it doens't paint it ;( but if...
11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
1
by: Bardo | last post by:
Hi all Ok first of me new to C#sharp. At the moment I am still learning the language. The problem I am running into is that I just can't seem to get a picture embedded into my program and then...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
1
by: jty202 | last post by:
I know alot of people have the problem with indexed pixel format. I hope someone can show me the solution to this.I am have problem with graphics with the following code giving the error: "A...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
2
by: JR | last post by:
Hi, I need to change the notifyicon in a countdown. after about 3000-5000 times there is an error. Here is all the code. what is wrong please help
2
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.