簡體   English   中英

System.InvalidOperationException:當前在其他地方使用對象

[英]System.InvalidOperationException: Object is currently in use elsewhere

嘗試在面板上繪制時,似乎出現此錯誤。 我不是C#專家,所以希望這里有人可以幫助我。 提前致謝。

堆棧跟蹤顯示

在C. \\ Users \\ kasunt \\ Microsoft Robotics Dev Studio 2008 R3 \\ Marvin \\ Teleoperation \\ MainForm.cs中的Victoria.Robotics.Marvin.Teleoperation.MainForm.DrawXYAxis(Graphics g)中的System.Drawing.Graphics.set_Transform(Matrix value) C:\\ Users \\ kasunt \\ Microsoft Robotics Dev Studio 2008 R3 \\ Marvin \\ Teleoperation \\ MainForm.cs中的Victoria.Robotics.Marvin.Teleoperation.MainForm.envMap_Paint(Object sender,PaintEventArgs e)中的第2173行:系統中的2143行。 System.Windows.Forms.Control.WmPaint(Message&m)的System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16層,布爾值disposeEventArgs)的Windows.Forms.Control.OnPaint(PaintEventArgs e) System.Windows.Forms.ScrollableControl.WndProc(Message&m)(位於System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)(位於System.Windows.Forms.Control.ControlNativeWindow)處的Forms.Control.WndProc(Message&m)。 System.Windows.Forms.NativeWindow.Callback上的WndProc(Message&m)(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

代碼如下。 設置轉換時似乎發生了錯誤,

    private void envMap_Paint(object sender, PaintEventArgs e)
    {
        DrawXYAxis(e.Graphics);
    }

    /// <summary>
    /// Helper to draw the XY axis and plot the map
    /// </summary>
    public void DrawXYAxis(Graphics g)
    {
        Rectangle rect = envMap.ClientRectangle;

        myPen = new Pen(Color.Black, 1);
        g.PageUnit = GraphicsUnit.Millimeter;
        g.PageScale = 0.1F;
        IntPtr hdc = g.GetHdc();
        int hMemDC = hdc.ToInt32();

        // Reverse the axis of the drawing surface
        Matrix mx = new Matrix(1, 0, 0, -1, 0, envMap.ClientSize.Height * 2);
        g.Transform = mx;
        g.TranslateTransform(50, 100, MatrixOrder.Append);

        // For drawing X - AXIS
        g.DrawLine(myPen, 0, 0, (2 * rect.Right - 60), 0);
        // For drawing Y - AXIS
        g.DrawLine(myPen, 0, 0, 0, 2 * rect.Bottom);

        // For drawing Arrow on X-AXIS
        g.DrawLine(myPen, (2 * rect.Right - 60) - 15, 8, (2 * rect.Right - 60), 0);
        g.DrawLine(myPen, (2 * rect.Right - 60), 0, (2 * rect.Right - 60) - 15, -8);

        // For drawing Arrow on Y-AXIS
        g.DrawLine(myPen, 8, 2 * rect.Bottom - 15, 0, 2 * rect.Bottom);
        g.DrawLine(myPen, 0, 2 * rect.Bottom, -8, 2 * rect.Bottom - 15);

        // Save the state to restore later
        GraphicsState state = g.Save();

        // Create a matrix to offset the text to the desired position and flip it the
        // right way up again
        Matrix mx2 = new Matrix(1, 0, 0, -1, 0, 0);
        Matrix mx1 = mx.Clone();
        mx1.Multiply(mx2);
        g.Transform = mx1;
        SolidBrush drawBrush = new SolidBrush(Color.Black);
        Font drawFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold);
        StringFormat sF = new StringFormat(StringFormatFlags.NoClip);
        sF.Alignment = StringAlignment.Center;
        g.DrawString("X", drawFont, drawBrush, (2 * rect.Right - 40), -2 * Font.Height, sF);
        g.DrawString("Y", drawFont, drawBrush, -40, -(2 * rect.Height + 2 * Font.Height), sF);

        // Restore state
        g.Restore(state);

        drawFont = new Font("Microsoft Sans Serif", 7);

        // Drawing Tick Marks and Labels
        // NOTE THE LABELS ON THE AXES WILL CHANGE TO REFFECT THE REAL POSITION OF THE ROBOT....
        myPen.Dispose();
   }

2173行,

        g.Transform = mx;

2143行,

        DrawXYAxis(e.Graphics);

這是一個異常,通常是由於在另一個線程中非法使用Graphics上下文引起的。 我不得不猜測您的程序中還有其他代碼也在處理圖形。

如果您不知道該代碼是什么,請使用Debug + Windows + Threads並瀏覽那里列出的線程的調用堆棧。 還要在源代碼中查找對Control.CheckForIllegalCrossThreadCalls的任何分配。 您將希望刪除該語句,以便在違反線程要求時得到更好的診斷。

原來是由於以下兩行,

    IntPtr hdc = g.GetHdc();
    int hMemDC = hdc.ToInt32();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM