从简单的表单显示System.ObjectDisposedException表单、简单、ObjectDisposedException、System

2023-09-07 01:07:24 作者:在吗

我试图找出如何在简要表示的WinForms对话框(如下code)出现以下异常和调用堆栈。这不会发生的事情,但我看到它在我的异常的记录。有任何想法吗?我无法弄清楚什么是引用一个释放的对象?

我已经验证(通过调用堆栈的其余部分),该应用程序没有关闭,它运行正常。

  System.ObjectDisposedException:无法访问已释放的对象。
对象名称:MainForm的。
   在System.Windows.Forms.Control.CreateHandle()
   在System.Windows.Forms.Form.CreateHandle()
   在System.Windows.Forms.Control.get_Handle()
   在System.Windows.Forms.Control.GetSafeHandle(IWin32Window窗口)
   在System.Windows.Forms.Form.ShowDialog(IWin32Window所有者)
   在MyApp.MainForm.PromptForProfile()
   在MyApp.MainForm.LoadProfile()
   在MyApp.MainForm.barButtonItem1_ItemClick(对象发件人,ItemClickEventArgs E)
 

这是在对话框中显示的code。唯一的傻瓜code可能是textPassword_KeyDown处理程序。我也许应该拉code我想出来的,而不是叫btnOK_Click的方式。

 公共部分类ProfileForm:DevEx press.XtraEditors.XtraForm
   {
      公共字符串_username;
      公共字符串_password;

      公共ProfileForm()
      {
         的InitializeComponent();
      }

      私人无效btnOK_Click(对象发件人,EventArgs的)
      {
         _username = textUsername.Text;
         _password = textPassword.Text;
      }

      私人无效textPassword_KeyDown(对象发件人,KeyEventArgs E)
      {
         如果(e.Key code == Keys.Enter)
         {
            btnOK_Click(发件人,NULL);
            this.DialogResult = DialogResult.OK;
            e.Handled =真实;
         }
      }

      私人无效hyperLinkEdit1_Click(对象发件人,EventArgs的)
      {
         //显示代理服务器设置对话框
         ProxyForm pform =新ProxyForm();
         pform.ShowDialog();
      }
   }
 

解决方案 简单表单提交练习

您的堆栈跟踪告诉我,你是不是进入了 ProfileForm code。它的一些控制的CreateHandle失败的。没有更多的信息,我只能猜测:

确认你执行所有的用户界面的操作是发生在您的GUI线程。即使你认为是,仔细检查。 (有时线程可微妙。)

请确保你没有尝试它已经处理完毕后显示相同的表单实例两次,第二次。我看到你有一个的ShowDialog()发生,但如果你想的ShowDialog()上这已经处理完毕的一种形式,我希望它爆炸这样的。

确保窗体上的任何用户控件行为正常。

考虑使用你的密码字段安全字符串。

I'm trying to figure out how when simply showing a WinForms dialog (code below) I get the following Exception and callstack. This doesn't happen all the time, but I'm seeing it in my exception logs. Any ideas? I can't figure out what would be referencing a disposed object?

I've verified (via the rest of the callstack) that the application is not shutting down, it is running normally.

System.ObjectDisposedException: Cannot access a disposed object.    
Object name: 'MainForm'.    
   at System.Windows.Forms.Control.CreateHandle()    
   at System.Windows.Forms.Form.CreateHandle()    
   at System.Windows.Forms.Control.get_Handle()    
   at System.Windows.Forms.Control.GetSafeHandle(IWin32Window window)    
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)    
   at MyApp.MainForm.PromptForProfile()    
   at MyApp.MainForm.LoadProfile()    
   at MyApp.MainForm.barButtonItem1_ItemClick(Object sender, ItemClickEventArgs e)

This is the code for the dialog being displayed. The only "goofy" code is probably the textPassword_KeyDown handler. I should probably pull the code I want out and not call btnOK_Click that way.

public partial class ProfileForm : DevExpress.XtraEditors.XtraForm
   {
      public string _username;
      public string _password;

      public ProfileForm()
      {
         InitializeComponent();
      }

      private void btnOK_Click( object sender, EventArgs e )
      {
         _username = textUsername.Text;
         _password = textPassword.Text;
      }

      private void textPassword_KeyDown( object sender, KeyEventArgs e )
      {
         if ( e.KeyCode == Keys.Enter )
         {
            btnOK_Click( sender, null );
            this.DialogResult = DialogResult.OK;
            e.Handled = true;
         }
      }

      private void hyperLinkEdit1_Click( object sender, EventArgs e )
      {
         // show the proxy settings dialog
         ProxyForm pform = new ProxyForm();         
         pform.ShowDialog();         
      }
   }

解决方案

Your stack trace tells me that you aren't getting into the ProfileForm code. It's failing on some control's CreateHandle. Without more information, I can only guess:

Verify that you're performing all your UI manipulation is occurring on your GUI thread. Even if you think it is, double check. (Sometimes the threading can be subtle.)

Make sure that you aren't trying to display the same form instance twice, the second time after it's already been disposed. I see that you've got a ShowDialog() happening, but if you're trying to ShowDialog() on a form that's already been disposed, I'd expect it to explode like this.

Ensure that any usercontrols on the form behave properly.

Consider using a secure string for your password field.