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

  • Question

  • 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;
    namespace WindowsFormsApplication1
        public partial class Form1 : Form
            Graphics curveGraphics;
            #region Data is received
            private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
    //.....Store the received values....//
                //Plot the stored data and then plot in the picture box

    And now the plot code:

           #region Plot data
            private void plotGraph(double BaseData)
                #region Graph Settings
                //Clear Screen
                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);
                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


    Tuesday, October 23, 2012 1:00 PM


All replies