Myworld
Myworld

Reputation: 1899

How do I dispose a form in a WinForms application?

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();
}

Upvotes: 1

Views: 3544

Answers (5)

Cody Gray
Cody Gray

Reputation: 245001

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();

Upvotes: 2

SWeko
SWeko

Reputation: 30932

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();

Upvotes: 2

ukhardy
ukhardy

Reputation: 2104

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

Upvotes: 0

codymanix
codymanix

Reputation: 29540

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?

Upvotes: 0

Fredrik Mörk
Fredrik Mörk

Reputation: 158389

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.

Upvotes: 1

Related Questions