开发者

How do I dispose a form in a WinForms application?

开发者 https://www.devze.com 2023-02-12 19:39 出处:网络
I had windows application which check if adobe acrobat installed in pc or not if it installed pdf file will display from cd if it is not installed installer window appear to setup the acrobat I did my

I had windows application which check if adobe acrobat installed in pc or not if it installed pdf file will display from cd if it is not installed installer window appear to setup the acrobat I did my code will but I want when pdf file run the windows form disposed I made that but this appeared:

Cannot access a disposed object. Object name: 'Checker'.

public Checker()
{
    InitializeComponent();
    Check();                          
}
//static void main()
//{

//    Checker ck = new Checker();
//    ck.Show();
//    Application.Run();
//}
public  void Check()
{
    try
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("AMD");

        if (adobe == null)
        {
            panel1.Show();
        }
        else
        {
            foreach (DriveInfo d in allDrives)
            {
                switch (d.DriveType)
                {
                    case DriveType.CDRom:
                        Process myProcess = new Process();
                        myProcess.StartInfo.FileName = d.Name + "Environment.docx";
                        myProcess.Start();

                        break;
                }
            }
        }
    }
    catch
    {
        MessageBox.Show("error occured");
 开发者_开发百科   }

    this.Dispose();
}


What you are doing, is creating a form, disposing it, and then showing it.

Using explicit disposes is rarely good, but your problem is that you are trying to use the disposed object after it is disposed, so the application crashes.

Try to close the form in the Check() method, using this.Close(), and use the using statement to ensure that the form will indeed be disposed when you are done with it, like this:

using (Checker ck = new Checker())
{
    ck.Show();
}
Application.Run();


In the overwhelming majority of cases, you should not call this.Dispose() in your code. By placing this call in a method called from the constructor, you're essentially disposing the current object out from underneath yourself. The run-time detects that, and presents you with an error when you try to access your form object on the next line by calling its Show method.

In this case, if you want to close your form after the Check method finishes, you should just call its Close method:

this.Close();


This happens because you call this.Dispose(); in the Check method, which is called from the constructor. Essentially you are disposing the form in the contructor, making it impossible to use.

It seems to me as if you should separate the logic a bit here. Make a method that performs the check, returning a bool indicating whether the form should be displayed or not, and then create and display the form only if should show up on the screen.


You are already disposing your form in the Check() method which is called in your constructor.

The show method is then executed, and at this stage, you object is already disposed.

Memory in .net is freed automatically, why do you want to call dispose explicitly?


Disposing the form is different from exiting the application. Try Application.Exit().

0

精彩评论

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

关注公众号