views:

116

answers:

2

I am using the System.Drawing.Graphics.DrawLines(Pen pen, PointF[] points) method in a multithreaded application, but the System.Drawing.Graphics isn't shared between threads.

Why it keeps throwing the System.InvalidOperationException: The object is currently in use elsewhere ?

+2  A: 

Simple answer: don't do that. Only access the GUI on the GUI thread.

Jon Skeet
It can happen in a GUI project. Please keep your answer so other users can benefit for it.
Jader Dias
A: 

The problem was: I was using the same System.Drawing.Pen instance for all threads. I had to clone it for every thread in order to solve the problem.

var pens = new Pen[0];
lock (this._pens)
{
    pens = (Pen[])this._pens.Select(a => (Pen) a.Clone()).ToArray();
}

Even the lock is essential in order to solve this problem

Jader Dias