locked
"Object is currently in use elsewhere" error for picturebox RRS feed

  • General discussion

  • Hello everyone,

    I created a GUI for plotting values in a picture box. I get values from a serial port every hour, store it in an array then plot those values using the drawlines function. My GUI works perfectly when I run it in Visual Studio (debug mode), but when I run the .exe my program will crash after receiving and plotting a few values (this is a random number of values) from the serial port, giving the error "object is currently in use elsewhere". I programmed the GUI using C#.

    I have read a lot of the threads and posts already but still cant solve my problem. I tried enabling the JIT as well but I can't get it working. Can anyone please help me?

    This a piece of my code:

    #region Namespace Inclusions
    using System;
    using System.Linq;
    using System.Data;
    using System.Text;
    using System.Drawing;
    using System.IO.Ports;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Threading;
    using System.IO;
    using WindowsFormsApplication1;
    using WindowsFormsApplication1.Properties;
    #endregion
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    
            Graphics curveGraphics;
    
            #region Data is received
            private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
            {
                curveGraphics.Flush();
                Delay2ms();
                Delay2ms();
    
    //.....Store the received values....//
    
                serialPort1.Close();
    
                //Plot the stored data and then plot in the picture box
                plotGraph(BaseData);
    
                curveGraphics.Flush();
                Delay2ms();
                Delay2ms();
                    
                serialPort1.Open();
         }
    }

    And now the plot code:

           #region Plot data
    
            private void plotGraph(double BaseData)
            {
                #region Graph Settings
                //Clear Screen
                curveGraphics.Clear(Color.White);
    
                //BackGround
                pictureBox1.Image = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
                curveGraphics = Graphics.FromImage(pictureBox1.Image);
    
                //Set the antialiasing property to smooth graph curve
                curveGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                //Plot a curve
                Pen blackPen = new Pen(Color.Black, 2.0F);
                Pen redPen = new Pen(Color.Red, 2.0F);
                Pen greenPen = new Pen(Color.Green, 2.0F);
                Pen yellowPen = new Pen(Color.Yellow, 2.0F);
    
                #endregion
    
                double Value = BaseData;
    
                #region Storing Values into an array to plot
    
                PowerCurve = storeDatainArray(Value);
    
    //...Code to store values into the proper array for plotting...//
    
                #region Plot New Powercurve
                curveGraphics.DrawLines(blackPen, CurveThis);
    
    }

    Thank you in advance

    Trinette



    • Changed type TrinetteK Monday, October 22, 2012 7:55 AM
    • Edited by TrinetteK Monday, October 22, 2012 7:55 AM
    Monday, October 22, 2012 7:32 AM

All replies

  • Hi Trinette,

    Thank you for posting in the MSDN forum.

    I’m afraid that it is not the correct forum for this issue, since this forum is to discuss the VS code analysis tool.

    Based on your description, if it is related to the specific winform app, if so, I suggest you post this issue in the winform forum for dedicated support. If it is a C# app, please post it in Visual C# General forum. Thanks for your understanding

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 23, 2012 10:02 AM
    Moderator