我在多线程应用程序中使用System.Drawing.Graphics.DrawLines(Pen pen, PointF[] points)
方法,但线程之间不共享System.Drawing.Graphics
。
为什么它一直在抛出System.InvalidOperationException: The object is currently in use elsewhere
?
答案 0 :(得分:2)
简单回答:不要那样做。仅访问GUI线程上的GUI。
答案 1 :(得分:2)
问题是:我为所有线程使用了相同的System.Drawing.Pen实例。我必须为每个线程克隆它以解决问题。
var pens = new Pen[0];
lock (this._pens)
{
pens = (Pen[])this._pens.Select(a => (Pen) a.Clone()).ToArray();
}
即使锁定也是必不可少的,以解决这个问题