473,289 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 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 3189
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.