C#错误(已处置对象) [英] C# Bug ( disposed object)

查看:82
本文介绍了C#错误(已处置对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;


命名空间 Mini_dbms
{
    公共 部分  class  Form1:表单
    {
        CreateDB.Form2 CR =  CreateDB.Form2();
        
        公共 Form1()
        {
            InitializeComponent();
        }

        私有 无效 ToolStripMenuItem_Click(对象发​​件人,EventArgs e)
        {

        }

        私有 无效 CreateToolStripMenuItem_Click(对象发​​件人,EventArgs e)
        {
           
            //  CreateDB.Form2 CR =新的CreateDB.Form2(); 
            CR.Show(); // 在重新打开它时出现错误<<未处理ObjcetDisposedException无法访问已处置的对象". >> 
       
        }

        私有 无效 Main_Load(对象发​​件人,EventArgs e)
        {
            
            CR.FormClosed + =  FormClosedEventHandler(CR_FormClosed);
           
        }

        无效 CR_FormClosed(对象发​​送者,FormClosedEventArgs e)
        {
            如果(CR.flag ==  1 )
            {
                toolStripStatusLabel1.Text = " ;
                
            }
            其他 如果(CR.flag ==  2 )
            {
                toolStripStatusLabel1.Text = " ;
            }
            
        }
    }
} 




当用户点击此事件时< CreateToolStripMenuItem> ,CR显示表格,但是当您关闭此表格并想
重新打开它,出现以下错误
<<未处理ObjcetDisposedException无法访问已处置的对象". >> :(

我应该在哪里获得对象(CR)的实例?:confused:
请注意,我使用了FormClosedEventHandler.
这是我的代码.

解决方案

错误表示它的意思.关闭表单后,您需要向该表单添加隐藏该表单而不是对其进行处理的代码.关闭事件需要停止关闭,然后才隐藏表单.否则,当您再次尝试显示它时,它已经被处理掉了,因此您可以准确地得到正在处理的错误.


您必须格外小心地声明在那里是C#中的错误,首先,如果这不是错误,则应在框架或Form类中说一个错误.

无论如何,这都不是一个错误,您要关闭它放置CR对象的形式,现在您仍具有对CR的引用,因为您在主表单的范围内对其进行了声明,请调用CR.Show() 答案4-因为您需要的是FormClosing而不是FormClosed,所以它具有 FormClosingEventArgs中的属性.因此,如果将Cancel设置为false,则不会关闭该窗体.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Mini_dbms
{
    public partial class Form1 : Form
    {
        CreateDB.Form2 CR = new CreateDB.Form2();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            //CreateDB.Form2 CR = new CreateDB.Form2(); 
            CR.Show();    //  error in here  while reopenig it<< ObjcetDisposedException  was unhandled  "cannot  access  a disposed object." >> 
       
        }

        private void Main_Load(object sender, EventArgs e)
        {
            
            CR.FormClosed += new FormClosedEventHandler(CR_FormClosed);
           
        }

        void CR_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (CR.flag == 1)
            {
                toolStripStatusLabel1.Text = "successfully created!";
                
            }
            else if(CR.flag==2)
            {
                toolStripStatusLabel1.Text = "Creation failed !";
            }
            
        }
    }
}




when user clicks on this event <CreateToolStripMenuItem > ,CR shows the form ,but when you close this form and wanna
reopen it ,below error occurs
<< ObjcetDisposedException was unhandled "cannot access a disposed object." >> :(

where shall I get the instace of the object (CR) ?:confused:
pay attention that I used the FormClosedEventHandler .
this is my code

解决方案

The error means what it says. When your form is closed, you need to add code that hides it rather than disposes of it, to that form. The closing event needs to STOP the close, then just hide the form. Otherwise, when you try to show it again, it''s been disposed of, and so you get exactly the error you''re dealing with.


You got to becarefull stating that there is a Bug in C#, first if this is bug which is not, you should say a bug in the framework or in Form class.

Anyway this is not a bug, you are closing the form of which it disposes the CR object, now you still have the reference to the CR as you are declaring it in the scope of the main form, you call the CR.Show() method which will give you an exception since the CR is already disposed.


re answer 4 - It''s FormClosing, not FormClosed that you need, it has a Cancel property in FormClosingEventArgs. So if you set Cancel to false, then the form will not be closed.


这篇关于C#错误(已处置对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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