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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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"....
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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)...
| |