在我的Windows窗体应用程序中,客户端有时会报告一个奇怪的异常:

System.InvalidOperationException: Value Dispose() cannot be called while doing CreateHandle()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at MyCompany.SomeApp.DialogBox.Show(string caption, string message)
   at MyCompany.SomeApp.MainForm.Button1_Click(Object sender, MouseEventArgs e)

导致此错误的代码如下所示:
namespace MyCompany.SomeApp
{
    public class DialogBox : CustomForm
    {
        public static DialogResult Show(string caption, string message)
        {
            using (DialogBox dialog = new DialogBox())
            {
                dialog.Text = caption;
                dialog.lblMessage.Text = message;
                return dialog.ShowDialog();
            }
        }
    }
 }

DialogBox本质上是一个继承自Windows.Forms.Form的类,并且进行了一些设计更改,没有什么特别的。异常发生在
return dialog.ShowDialog();

而不是我所期望的using块的末尾。看起来好像是在ShowDialog()方法内,并且在创建窗体的句柄之前,调用了Dispose()方法。
但是我的DialogBox既不调用Dispose()本身也不吞咽其他异常,它仅在OnPaint()事件中进行一些绘制。

有人有一些线索可以摆脱这个异常(exception)吗?

更新:

这是我的CustomForm类中的唯一代码(除了Windows窗体设计器中的更改模式(添加了2个Label,一个按钮并更改了一些颜色)
Public Class CustomForm
    Inherits System.Windows.Forms.Form

    <DebuggerStepThrough()> _
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Static pen1 As New Pen(Color.FromArgb(39, 46, 54), 21)
        Static pen2 As New Pen(Color.FromArgb(44, 51, 59), 1)
        Static pen3 As New Pen(Color.FromArgb(93, 99, 99), 1)
        Static pen4 As New Pen(Color.FromArgb(119, 124, 127), 1)
        Static pen5 As New Pen(Color.FromArgb(148, 157, 156), 1)
        Static pen6 As New Pen(Color.FromArgb(175, 185, 186), 1)

        With e.Graphics
            .DrawRectangle(Pens.Black, 0, 0, (Me.Width - 1), (Me.Height - 1))
            .DrawLine(pen1, 1, 11, Me.Width - 1, 11)
            .DrawLine(pen2, 1, 22, Me.Width - 2, 22)
            .DrawLine(pen3, 1, 23, Me.Width - 2, 23)
            .DrawLine(pen4, 1, 24, Me.Width - 2, 24)
            .DrawLine(pen5, 1, 25, Me.Width - 2, 25)
            .DrawLine(pen6, 1, 26, Me.Width - 2, 26)
        End With
    End Sub

    Private Const GWL_STYLE As Integer = (-16)
    Private Const WS_CAPTION As Integer = &HC00000
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
             (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public ReadOnly Property HasCaption() As Boolean
        Get
            return (GetWindowLong(Me.Handle, GWL_STYLE) And WS_CAPTION) = WS_CAPTION
        End Get
    End Property

End Class

更新:我更改了代码一点,因为它具有误导性。
在堆栈跟踪中,您可以看到My.App.DialogBox.Show(...)被调用。这是一个静态方法,其中包含using块。
DialogBox noCustomForm都不会覆盖Form的ShowDialog()方法(这就是为什么它不显示在stacktrace中的原因)。

最佳答案

认为我在这里找到了一个Clou:http://softwareinvent.com/wordpress/?p=10



看起来完全像我的问题。也许是一些奇怪的线程问题,在这种情况下,在极少数情况下,会在查询属性HasCaption的同时恰好同时调用HasCaption属性。

10-08 01:24