473,396 Members | 1,860 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,396 software developers and data experts.

Object is currently in use elsewhere.

Hello. This is my first question asked in here, since I'm pretty new to c# programming. So I hope I write it in a proper understandable way.

My problem is, that I have a program with sveral classes, and I want a bitmap sent from my graphic class to the main class, when an event occurs in the graphic class. The program runs fine for a while (2-3) seconds, but then I get this message: "Object is currently in use elsewhere" and it is the bitmap it is referring to.

It is a school projekt to make a digital oscilloscope, so there is a lot of code to the projekt. Therefore it would be confusing if i copy'ed it all in here. So I will try to just copy the important parts.

Hope that you can help me :)

thanks.



Main class:
Expand|Select|Wrap|Line Numbers
  1. public Main()
  2.         {
  3.             Graphic.InitGraphic();
  4.             Graphic.ChangeOfGrapic += new Graphic.ChangingHandler(GraphicChange);
  5.             Background.Controls.Add(Graph);
  6.             Graph.BackColor = Color.Transparent;
  7.             Graph.Location = new System.Drawing.Point(0, 0);
  8.             Graphic.DrawBackground();
  9.         }
  10.  
  11.         private void GraphicChange(Bitmap ChangedGrapic)
  12.         {
  13.             Graph.Image = ChangedGrapic;
  14.         }
  15.  
  16.  
  17. Here I have to add that Graph and Background are picturebox'es in my form.
  18.  
  19.  
  20.  
  21. Grapic class:
  22.     static class Graphic
  23.     {
  24.         public static int x = 0;
  25.         private static Bitmap myBitmapBackground = new Bitmap(600, 600);
  26.         private static Graphics myGraphicsBackground;
  27.         private static Bitmap myBitmapGraph = new Bitmap(600, 600);
  28.         private static Graphics myGraphicsGraph;
  29.  
  30.         public static event ChangingHandler ChangeOfGrapic;
  31.  
  32.         public delegate void ChangingHandler(Bitmap test);
  33.  
  34.         public static void InitGraphic()
  35.         {
  36.             myGraphicsBackground = Graphics.FromImage(myBitmapBackground);
  37.             myGraphicsGraph = Graphics.FromImage(myBitmapGraph);
  38.         }
  39.  
  40.  
  41.  
  42.         public static void ShowMeasurement(float measvalue)
  43.         {
  44.             if (x >= 600 && Functions.mode == 0)
  45.             {
  46.                 ClearGraph();
  47.             }
  48.  
  49.             else if (x >= 600 && Functions.singlerun == false)
  50.             {
  51.                 if (Functions.timebase == 10 || Functions.timebase == 1 || Functions.timebase == 0.1)
  52.                 {
  53.                     ClearGraph();
  54.                 }
  55.             }
  56.  
  57.             else
  58.             {
  59.                 SolidBrush graphdot = new SolidBrush(Color.Red);
  60.  
  61.  
  62.                 myGraphicsGraph.FillEllipse(graphdot, x, (300 - ((300 / 5) * ((measvalue + Functions.offset) / Functions.voltagescale))), 2, 2);
  63.             }
  64.  
  65.             if (Functions.timebase == 0.1)
  66.             {
  67.                 x += 6;
  68.             }
  69.  
  70.             else
  71.             {
  72.                 x += 1;
  73.             }
  74.  
  75.             ChangeOfGrapic(myBitmapGraph);
  76.         }


It seems to me that both the Main and the Grapic class is using the bitmaps in some sort of way. But correct me if I'm wrong, but they run in the same thread, right?
Nov 26 '09 #1
6 18480
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Nov 26 '09 #2
tlhintoq
3,525 Expert 2GB
Make a clone of the image then send that. Each class will have its own bitmap to work with.

Expand|Select|Wrap|Line Numbers
  1. Image NewImage = (Image)OldImage.Clone();
I *think* you can also 'lock' the image so one class will have to wait for the other to be done, but I haven't done much with that so I'm not sure.
Nov 26 '09 #3
Ok, thanks for the tips. I have allready tryed cloning the bitmap before sending it. It looked like this:

Expand|Select|Wrap|Line Numbers
  1. Bitmap copy = (Bitmap) myBitmapGraph.Clone();
  2. ChangeOfGrapic(copy);
It did help to make the program run for a little longer before throwing the "object is currently in use elsewhere". But instead it was way slower as well. I want to refresh the bitmap about 30 times a second, so it cant be slow.
Actually to start with I made the whole program in one class. And it worked like a charm... But my teacher want me to devide it into classes, so here comes the trouble..
Nov 26 '09 #4
GaryTexmo
1,501 Expert 1GB
Hmmm, instead of using the Clone, give this a try...

Expand|Select|Wrap|Line Numbers
  1. Bitmap copy = Bitmap.FromHbitmap(myBitmapGraph);
In another thread a Clone was suggested on a bitmap but it didn't remove the file handle, perhaps it's the same here?
Nov 26 '09 #5
Hey. I tryed what you suggested. My code looked like this:

Expand|Select|Wrap|Line Numbers
  1. IntPtr test = myBitmapGraph.GetHbitmap();
  2.             Bitmap copy = Bitmap.FromHbitmap(test); 
  3.             ChangeOfGrapic(copy);
I worked for a little while (30 sek) which was a new record, but it was super slow, so not a good solution. After the 30 seconds this error occurred:

A generic error occurred in GDI+.

But thanks for the input!
Nov 27 '09 #6
GaryTexmo
1,501 Expert 1GB
Oops, yes your code is correct, I obviously missed the GetHbitmap call :)

Is your image animated, by any chance? I've seen that generic GDI+ error before... it's rather irritating. I've ran into it twice. The first time was when I used the above method with animated gifs and the other was the other day when I loaded an incorrect image format into the Bitmap class.

Unfortunately that error is absolutely useless so far as I can tell. There's no inner exception with more details, though you do get a stack trace and it's crashing in the System.Drawing class, inside the code for an animated image.
Nov 27 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: talljames | last post by:
Has anyone come across this exception before when dealing with C# graphics? Have come across some answers that point to the accelleration control under the screen's...
4
by: 6tc1 | last post by:
Hi all, I have just finished debugging a windows application and have solved the problem - however, I want to be sure that I understand the problem before I move on. Before I detail the problem,...
0
by: Sam Barham | last post by:
I have a ListView control, for which I have overwritten the WndProc method to gain access to the WM_PAINT message and generate my own OnPaint and OnPaintBackground messages, in order to colour the...
1
by: Hadar | last post by:
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...
1
by: kCura | last post by:
First off, I'm sorry if this isn't the write thread to post this message but I couldn't find one dedicated to System.Drawing. I'm developing an intranet application in VB.NET that uses a Hosted...
4
by: Jos Lavrijsen | last post by:
When i try to re-use brushes or pens over multiple threads, i often get an InvalidOperationException with the message 'object is currently in use elsewhere'. I also tried this by re-using fonts...
4
by: JJ | last post by:
When I run my form prog, if I maximise, then minimise (i..i. force the GUI to redraw) I get the following error: "The object is currently in use elsewhere" At this line: ...
4
by: Mau Kae Horng | last post by:
Hello, I have a C# Windows Forms application for machine. Due to some unknown reasons, the application face problems with unexpected exceptions happening, resulting in two red lines forming a...
4
by: =?Utf-8?B?Qm9uaQ==?= | last post by:
Hi, I got this problem. I'm implementing a pluggable winform program. My plugins are usercontrol and I load them in my program through a interface. Now if I close my application an error occurs:...
0
by: para15000 | last post by:
Hello I have a C# desktop application in which one thread that I create continously gets an image from a source(it's a digital camera actually) and puts it on a panel(panel.Image = img) in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.