Jür*_*ock 5 .net c# dispose winforms
在我的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)
Run Code Online (Sandbox Code Playgroud)
导致此错误的代码如下所示:
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
DialogBox基本上是一个继承自Windows.Forms.Form的类,并做了一些设计更改,没什么特别的.例外情况发生在
return dialog.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
而不是using像我期望的那样在街区尽头.看起来在某种程度上,在ShowDialog()方法中,在创建表单句柄之前,调用Dispose()方法.但我的DialogBox既没有调用Dispose()本身也没有吞下其他异常,它只在OnPaint()事件中做了一些绘画.
有没有人有一些线索如何摆脱这个例外?
更新:
这是我的CustomForm类中唯一的代码(除了Windows窗体设计器中的更改模式(添加了2个标签,一个按钮并更改了一些颜色)
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
Run Code Online (Sandbox Code Playgroud)
更新:我稍微更改了代码,因为它具有误导性.
在堆栈跟踪中,您可以看到调用了My.App.DialogBox.Show(...).哪个是包含using块的静态方法.
DialogBox nore CustomForm都不会覆盖Form的ShowDialog()方法(这就是为什么它没有显示在stacktrace中.
我想我在这里找到了一个要点:http://softwareinvent.com/wordpress/?p =10
\n\n\n\n\n今天我不断收到一个奇怪的异常:
\n\n执行 CreateHandle() 时\n 无法调用 Value Dispose()
\n\n我无法\xe2\x80\x99弄清楚是什么原因导致\n,直到我开始注释\n大段代码(必须喜欢暴力\n强制调试)。这一切都归结于一行看起来非常无辜的代码:
\n\nRun Code Online (Sandbox Code Playgroud)\n\nif (Handle != IntPtr.Zero)\n这行代码获取非托管 Windows 句柄。因此,必须手动清理该句柄
\n
看起来和我的问题一模一样。也许存在一些奇怪的线程问题,在极少数情况下,在查询属性 HasCaption 的同时调用 HasCaption 属性。
\n