开发者

Cannot access a disposed object?

I have a countdown Timer form - on the first form the user will enter the countdown time - warning times, end message, etc. There are also two Radio buttons (Max/Min) and depending on which is selected they will open a new Max or Min form where the time will actually start to countdown. It is working fine and counting down as I expect. However, if I exit the Max or Min form and try to run again with new times I get the error. The code is below - note the comment out ShowDialog(this); was something I tried - it let me close and open the new forms ok but it did not actually start the countdown. UpdateLabels is the function that does the updating of Labels.

                bool Max = rbMax.Checked;
                if (Max == true)
                {
                    //_Max.ShowDialog(this);
                    _Max.Show();

                }
                else
                    //_Min.ShowDialog(this);
                    _Min.Show();

        开发者_C百科        UpdateLabels();
            }

I also tried the following which I read online as a possible solution but it also did not work...

    private void Max_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.Hide();
        this.Parent = null;
    }

Can anyone help me out - I can post the UpdateLabels function if needed. I am pretty new to UI C# development so any help would be great. Thanks.


The problem is, that a closed form can not be used anymore (be reopened). Thats why the code you posted tries to stop closing and only hides your window. But for doing this, the Cancel-property must be set to true:

private void Max_FormClosing(object sender, FormClosingEventArgs e)    {        
   this.Hide();        
   this.Parent = null;    
   e.Cancel=true;
}

To show the form after closing it this way, show it with the Show() method.

However this is probably only a workaround and you could solve the problem with another design. Maybe it would be wise, to create a new instance of your form, every time you need it, instead of trying to reopen it every time. This also has the advantage that the form only requesires resources if it is really needed.


What you can do is add a following check before calling .Show method:

if(_Max == null || _Max.IsDisposed)
    _Max = new MaxForm();       

_Max.Show();

and similarly for _Min form


Whenever a form is closed, all its resources are freed. This means that you can't access the object any more, since it no longer exists - it's been freed and deleted from memory. To prevent that, you can cancel the closing of the form, and hide it instead (which will appear transparent to the user).

this.Hide();        
e.Cancel=true;

An updated version of your code is as follows:

private void Max_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Hide();
    this.Parent = null;
}


The solution is simple that instantiate the object of the called form in the button click event e.g.

private void buttonSetting_Click( object sender, EventArgs e )
    {
        ***_setting = new SettingWindow();***  //When I need to show the settings window

        _setting.Show();
    } 


create new instatnce if object is not available

if(frmRGB==nullptr || frmRGB.IsDisposed==true ) { frmRGB= new Form(); }


Create Object inside button click event like this

private void btn_supplier_order_Click(object sender, EventArgs e)
        {
            form_supplier_order so = new form_supplier_order();
            so.Show();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜