"InvalidOperationException:当前在其他地方使用对象".在无害的上漆期间? [英] "InvalidOperationException: Object is currently in use elsewhere" during innocuous onpaint?

查看:172
本文介绍了"InvalidOperationException:当前在其他地方使用对象".在无害的上漆期间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我们收到"InvalidOperationException:对象在其他地方当前正在使用".

For some reason we are getting "InvalidOperationException: Object is currently in use elsewhere."

在下面的自定义OnPaint期间(实际上几乎是代码行复制的一行...那里几乎没有).

during our custom OnPaint, below (that's actually almost a line for line copy of the code... there's that little there).

我们已经在下面登录了异常处理程序,以检测是否以某种方式从非UI线程调用OnPaint ...并且不会被触发,但是我们正在记录该错误(请参见下面的堆栈跟踪) .

We have logging in the exception handler below to detect if we're somehow calling OnPaint from a non-UI thread... and that isn't getting tripped, but we are getting that error logged (see stack trace below).

在遇到这些错误的计算机上,我们还看到其他控件(可能没有对它们的OnPaint进行尝试/捕捉)所带来的可怕的Red X厄运.

On machines where we're getting these errors, we're also seeing the dreaded Red X of doom from other controls (which presumably don't have a try/catch around their OnPaints).

它们可能是相关的,但是我无法弄清楚如果仅从UI线程调用此代码会导致该错误的原因.

They're probably related, but I can't figure out what could be causing that error if this code is only called from the UI thread.

有什么想法吗?

这是堆栈跟踪:

This is the stack trace:

System.InvalidOperationException:当前正在使用对象
,位于System.Drawing.Graphics.CheckErrorStatus(Int32 状态)
在System.Drawing.Graphics.DrawRectangle(钢笔,Int32 x,Int32 y,Int32宽度,Int32高度)
在 System.Windows.Forms.ControlPaint.DrawBorderSimple(图形图形, 矩形边界,颜色,ButtonBorderStyle样式)
在 System.Windows.Forms.ControlPaint.DrawBorder(图形图形, 矩形边界,颜色,ButtonBorderStyle样式)
在 MyUserControl.OnPaint(PaintEventArgs e)

System.InvalidOperationException: Object is currently in use elsewhere.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawRectangle(Pen pen, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Windows.Forms.ControlPaint.DrawBorderSimple(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style)
at System.Windows.Forms.ControlPaint.DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style)
at MyUserControl.OnPaint(PaintEventArgs e)

这是课程:

public class MyUserControl : UserControl
{
    // Override this to set your custom border color
    protected Color mBorderColor = SystemColors.ControlDarkDark;

    public MyeUserControl()
        : base()
    {
        this.BorderStyle = BorderStyle.None;
        this.Padding = new Padding(1);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        try
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, mBorderColor, ButtonBorderStyle.Solid);
        }
        catch (Exception ex)
        {
            // check if we're not on the UI thread, and if not, log it
            // log exception
        }
    }
}

推荐答案

所以,我前段时间已经弄清楚了,但是忘了在这里给出答案了.所有遇到问题的客户都有一个共同点-他们安装了一个名为FileOpen的adobe插件.它允许用户阅读加密的PDF.事实证明,在Windows GDI +调用(从.Net OnPaint方法调用)期间引发异常时,FileOpen正在进行的操作(可能是阻止对加密的PDF的屏幕截图或某些操作)干扰了我们的应用程序.在使用FileOpen时,他们将我们的应用程序列入了白名单,这样就不会阻止来自我们应用程序的GDI +调用.

So, I figured this out some time ago, but forgot to put the answer on here. All the customers with the problem had a single thing in common - they had installed an adobe plugin called FileOpen. It allows users to read encrypted PDFs. Turns out something FileOpen was doing (presumably to block screen captures of encrypted PDFs or something) was interfering with our application, by throwing exceptions during windows GDI+ calls (which get called from .Net OnPaint methods). In working with FileOpen, they whitelisted our application so that they wouldn't block GDI+ calls from our application.

要弄清楚这一点的棘手之处在于,仅在您第一次使用FileOpen查看加密的PDF之后才发生阻塞,因此您可以安装它而不会遇到问题.如果停止他们的Windows服务FileOpenBroker,它也可以解决该问题(大概是该服务正在执行阻止操作).

What made this even more tricky to figure out, is that the blocking only happens after the first time you view an encrypted PDF using FileOpen... so you can have it installed and not experience the problem. If you stop their windows service FileOpenBroker, it also fixes the problem (presumably the service is what is doing the blocking).

只是将其发布在这里,以防其他人遇到相同的问题,因为这对我们的工作来说是一个巨大的麻烦,并且花了数周的时间才弄清.

Just posting this on here in case anyone else sees the same problem, since this was a huge headache for us at work, and took weeks to figure out.

更新: 有一个非常简单的解决方法,即停止FileOpen的服务,该服务称为FileOpenBroker.您应该可以在Windows服务列表中找到它,也可以在Windows任务管理器中找到它.一旦该过程停止,它将停止他们为破坏GDI +所做的一切,然后,您应该可以使用该程序,直到下次打开加密的PDF.

Update: There's a pretty easy workaround, which is to stop FileOpen's service, which is called FileOpenBroker. You should be able to find it in the list of windows services and as a process in the windows task manager. Once the process has been stopped, it stops whatever they're doing to screw up GDI+, and then you should be able to use your program until the next time you open an encrypted PDF.

已经有一段时间了,所以我不确定,但是可能需要重新启动才能释放他们在GDI +中设置的任何锁.我记得我建立了一个批处理文件来启动和停止该服务,以便您可以在不完全禁用使用FileOpen的情况下使用程序(我假定已安装该文件,因为该计算机上正在使用它).

It's been a while, so I don't remember for sure, but it's possible a reboot was necessary to release whatever locks they put in GDI+. I remember I built a batch file to start and stop the service, so that you could use your program without completely disabling the ability to use FileOpen (which I presume is installed because it's in use on that computer).

我刚刚遇到其他遇到同样问题的人,所以FileOpen似乎并没有解决根本问题-他们只是通过将我们的特定应用列入白名单而对它进行了创可贴...公平的警告.

I just got contacted by someone else hitting this same problem, so it seems like FileOpen hasn't fixed the root problem - they only put a band-aid on it by whitelisting our particular application... fair warning.

这篇关于"InvalidOperationException:当前在其他地方使用对象".在无害的上漆期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆