Object is currently in use elsewhere.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Askelassen
    New Member
    • Nov 2009
    • 6

    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:
    Code:
    public Main()
            {
                Graphic.InitGraphic();
                Graphic.ChangeOfGrapic += new Graphic.ChangingHandler(GraphicChange);
                Background.Controls.Add(Graph);
                Graph.BackColor = Color.Transparent;
                Graph.Location = new System.Drawing.Point(0, 0);
                Graphic.DrawBackground();
            }
    
            private void GraphicChange(Bitmap ChangedGrapic)
            {
                Graph.Image = ChangedGrapic;
            }
    
    
    Here I have to add that Graph and Background are picturebox'es in my form.
    
    
    
    Grapic class:
        static class Graphic
        {
            public static int x = 0;
            private static Bitmap myBitmapBackground = new Bitmap(600, 600);
            private static Graphics myGraphicsBackground;
            private static Bitmap myBitmapGraph = new Bitmap(600, 600);
            private static Graphics myGraphicsGraph;
    
            public static event ChangingHandler ChangeOfGrapic;
    
            public delegate void ChangingHandler(Bitmap test);
    
            public static void InitGraphic()
            {
                myGraphicsBackground = Graphics.FromImage(myBitmapBackground);
                myGraphicsGraph = Graphics.FromImage(myBitmapGraph);
            }
    
    
    
            public static void ShowMeasurement(float measvalue)
            {
                if (x >= 600 && Functions.mode == 0)
                {
                    ClearGraph();
                }
    
                else if (x >= 600 && Functions.singlerun == false)
                {
                    if (Functions.timebase == 10 || Functions.timebase == 1 || Functions.timebase == 0.1)
                    {
                        ClearGraph();
                    }
                }
    
                else
                {
                    SolidBrush graphdot = new SolidBrush(Color.Red);
    
    
                    myGraphicsGraph.FillEllipse(graphdot, x, (300 - ((300 / 5) * ((measvalue + Functions.offset) / Functions.voltagescale))), 2, 2);
                }
    
                if (Functions.timebase == 0.1)
                {
                    x += 6;
                }
    
                else
                {
                    x += 1;
                }
    
                ChangeOfGrapic(myBitmapGraph);
            }


    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?
    Last edited by tlhintoq; Nov 26 '09, 05:18 PM. Reason: [CODE] ...your code here... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Make a clone of the image then send that. Each class will have its own bitmap to work with.

      Code:
      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.

      Comment

      • Askelassen
        New Member
        • Nov 2009
        • 6

        #4
        Ok, thanks for the tips. I have allready tryed cloning the bitmap before sending it. It looked like this:

        Code:
        Bitmap copy = (Bitmap) myBitmapGraph.Clone();
        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..

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Hmmm, instead of using the Clone, give this a try...

          Code:
          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?

          Comment

          • Askelassen
            New Member
            • Nov 2009
            • 6

            #6
            Hey. I tryed what you suggested. My code looked like this:

            Code:
            IntPtr test = myBitmapGraph.GetHbitmap();
                        Bitmap copy = Bitmap.FromHbitmap(test); 
                        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!

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              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.

              Comment

              Working...