[英]InvalidOperationException on Winforms Calling Base.Dispose() when closing window
Hello im pretty new to c# just downloaded VS2012 and created my new Application, but im getting a really strange Exception, I guess its my fault but again im really new to this 您好,我是c#的新手,刚刚下载了VS2012并创建了我的新应用程序,但是我收到了一个非常奇怪的异常,我想这是我的错,但是对于这个我真的很新
the exception occours when closing form2 that form1 created. 关闭由form1创建的form2时发生异常。 this only occurs when an object its placed on form2.
仅当将对象放置在form2上时才会发生这种情况。
I just got 2 forms
with a button on each, button on form1 calls form2, when form2 is closed I show again form1, after a few seconds it throws InvalidOperationException
on line base.Dispose 我刚得到
2 forms
,每个2 forms
上都有一个按钮,form1上的按钮调用form2,当form2关闭时,我再次显示form1,几秒钟后它在行base上抛出InvalidOperationException
。
here is the code that trhows the exception 这是解决异常的代码
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing); // here is the exceptjion
}
Here is form1 这是form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show(this);
this.Hide();
}
}
Here is form2 这是form2
public partial class Form2 : Form
{
private Form frm;
public Form2(Form frm) : this()
{
this.frm = frm;
}
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
frm.Show();
this.Close();
}
}
Here is the stacktrace 这是堆栈跟踪
No se controló System.InvalidOperationException
HResult=-2146233079
Message=Operación no válida a través de subprocesos: Se tuvo acceso al control 'button1' desde un subproceso distinto a aquel en que lo creó.
Source=System.Windows.Forms
StackTrace:
en System.Windows.Forms.Control.get_Handle()
en System.Windows.Forms.Control.get_InternalHandle()
en System.Windows.Forms.Control.DestroyHandle()
en System.Windows.Forms.Control.Dispose(Boolean disposing)
en System.Windows.Forms.ButtonBase.Dispose(Boolean disposing)
en System.ComponentModel.Component.Dispose()
en System.Windows.Forms.Control.Dispose(Boolean disposing)
en System.Windows.Forms.Form.Dispose(Boolean disposing)
en PruebaExceocion.Form2.Dispose(Boolean disposing) en c:\Users\Alex\Documents\Visual Studio 2012\Projects\PruebaExceocion\PruebaExceocion\Form2.Designer.cs:línea 20
en System.ComponentModel.Component.Dispose()
en System.Windows.Forms.Form.WmClose(Message& m)
en System.Windows.Forms.Form.WndProc(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
Cancel it and hide it.. like so: 取消并隐藏它..像这样:
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
this.Hide();
frm.Show();
}
If you just simply program with 2 forms, then calling one from the other's button can be done also simply like 如果您只是简单地用2种形式编程,那么可以通过另一个按钮调用其中一种,就像
//On form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
And no more, you can close the second form whenever you want without any error (I tested this on vs12). 而且,您可以随时关闭第二个表单,而不会出现任何错误(我在vs12上进行了测试)。 It will create a new instance of the Form2 with each button click.
每次单击按钮都会创建一个Form2的新实例。 So it becomes a bit trickier if you only want one and the same instance of form2 displayed with each click.
因此,如果您只希望每次单击都显示一个相同的form2实例,那将变得有些棘手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.