object is currently in use elesewhere
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: object is currently in use elesewhere

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    Exclamation object is currently in use elesewhere

    hello every body,
    i have a small project, in which i am using Two Threads, and a method to draw circle on an image. when it starts so after some time it gives the error message object is currently in use elsewhere. sample of code is been give blow please, if anyone could give me some guidelines or some idea please.
    Code:
    private delegate void DrawCircleDelegate(Point pp, Bitmap r);
            public void DrawCircle(Point p, Bitmap rate)
            {
                if (InvokeRequired)
                {
                    DrawCircleDelegate drwCircle = new DrawCircleDelegate((pp,r)=> DrawCircle(p,rate));
                    this.BeginInvoke(drwCircle, new object[] { p,rate });
                }
                // utilObject.StateOccure += new SimUtil.InvalidState(utilObject_StateOccure); // Event handler , to handle invalid states
                Bitmap tempbitmap = (Bitmap)testImage.Clone();
                Graphics g = Graphics.FromImage(tempbitmap);
     
                if (rate== null )
                    g.FillEllipse(Brushes.Green, p.X - 15, p.Y - 15, 30, 30);            
                else 
                    g.DrawImage(rate, p.X - 15, p.Y - 15, 30, 30);
     
                pictureBox1.Image = tempbitmap;
     
            }
    private void frmTwoThreads_Load(object sender, EventArgs e)
            {
                testImage = new Bitmap(@"D:\Practice_Projects\TestPic.png");
                rnd = new Random(DateTime.Now.Second);
                findNextThread = new Thread(FindNext);
                findNextThread.Name = " Find Thread";
     
            }
    private void btnStarSimulator_Click(object sender, EventArgs e)
            {
                utilObject = new SimUtil();
                //rnd = new Random(DateTime.Now.Second);
               // List<Cell> gridCell = new List<Cell>();
                myGrid = new Grid(row, col, new Point(10, 10), new Point(460, 440));
     
                for (int clNo = 1; clNo <= myGrid.TotalCells; clNo++)
                {
                    Cell tempCell = new Cell(clNo, col, row, myGrid.InitialPoint, myGrid.GridSize);
                    myGrid.Cells.Add(tempCell);
                }
                testImage = myGrid.DrawGridOnBitmap(testImage, myGrid.InitialPoint, myGrid.EndPoint);
                pictureBox1.Image = testImage;
                DrawCircle(Get_CellCenterPoint(startCell),null);          
     
    
                findNextThread.Start();
            }
      public void FindNext()
            {
     
                while (true)
                {
     
                    //moveCircleDelegate mcircle = moveCircle;               
                    Point fromCellPoint;
                    Point toCellPoint;
                    int nextLocation = utilObject.SelectNextCell(startCell, rnd);
                    while (nextLocation  <= 0 || nextLocation  > 100)
                    {
                        string s1 = startCell.ToString() + " , " + nextLocation.ToString();
                        this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(s1); });  
                        nextLocation = utilObject.SelectNextCell(startCell, rnd);
                    }
                                  
                        fromCellPoint = Get_CellCenterPoint(startCell);
                        toCellPoint = Get_CellCenterPoint(nextLocation);
                        string s = startCell.ToString() + " , " + nextLocation.ToString() ;
                        this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(s); });             
                         moveCircleThread = new Thread(() => moveCircle(fromCellPoint, toCellPoint, 100));
                         moveCircleThread.Start();
                         moveCircleThread.Join();
                         startCell = nextLocation;
                }
            }

  2. #2
    Join Date
    Nov 2013
    Posts
    2

    Re: object is currently in use elesewhere

    error come in DrawCircle Method.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,920

    Re: object is currently in use elesewhere

    you can't draw to the same bitmap at the same time from 2 threads, you need synchronisation.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center