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:
我有windows应用程序检查adobe acrobat是否安装在PC中如果安装了pdf文件将从cd显示如果没有安装安装程序窗口出现设置acrobat我做了我的代码将但我想pdf文件运行Windows窗体处方我做了但是这个出现了:
Cannot access a disposed object. Object name: 'Checker'.
无法访问已处置的对象。对象名称:'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();
}
5 个解决方案
#1
2
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:
尝试使用this.Close()关闭Check()方法中的表单,并使用using语句确保在完成表单时确实将处理该表单,如下所示:
using (Checker ck = new Checker())
{
ck.Show();
}
Application.Run();
#2
2
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.
在绝大多数情况下,您不应该在代码中调用this.Dispose()。通过将此调用放在从构造函数调用的方法中,您实际上是将当前对象从您自己的下方处理出来。运行时检测到该错误,并在尝试通过调用其Show方法访问下一行的表单对象时向您显示错误。
In this case, if you want to close your form after the Check
method finishes, you should just call its Close
method:
在这种情况下,如果要在Check方法完成后关闭表单,则应该只调用其Close方法:
this.Close();
#3
1
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.
这是因为你调用this.Dispose();在Check方法中,从构造函数调用。基本上你是在构造函数中处理表单,使其无法使用。
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.
在我看来,好像你应该把逻辑分开一点。创建一个执行检查的方法,返回一个bool,指示是否应该显示表单,然后只有在屏幕上显示时才创建和显示表单。
#4
0
You are already disposing your form in the Check() method which is called in your constructor.
您已经在构造函数中调用的Check()方法中处理表单。
The show method is then executed, and at this stage, you object is already disposed.
然后执行show方法,在此阶段,您已经处理了对象。
Memory in .net is freed automatically, why do you want to call dispose explicitly?
.net中的内存是自动释放的,为什么要显式调用dispose?
#5
0
Disposing the form is different from exiting the application. Try Application.Exit().
处理表单与退出应用程序不同。尝试Application.Exit()。
#1
2
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:
尝试使用this.Close()关闭Check()方法中的表单,并使用using语句确保在完成表单时确实将处理该表单,如下所示:
using (Checker ck = new Checker())
{
ck.Show();
}
Application.Run();
#2
2
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.
在绝大多数情况下,您不应该在代码中调用this.Dispose()。通过将此调用放在从构造函数调用的方法中,您实际上是将当前对象从您自己的下方处理出来。运行时检测到该错误,并在尝试通过调用其Show方法访问下一行的表单对象时向您显示错误。
In this case, if you want to close your form after the Check
method finishes, you should just call its Close
method:
在这种情况下,如果要在Check方法完成后关闭表单,则应该只调用其Close方法:
this.Close();
#3
1
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.
这是因为你调用this.Dispose();在Check方法中,从构造函数调用。基本上你是在构造函数中处理表单,使其无法使用。
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.
在我看来,好像你应该把逻辑分开一点。创建一个执行检查的方法,返回一个bool,指示是否应该显示表单,然后只有在屏幕上显示时才创建和显示表单。
#4
0
You are already disposing your form in the Check() method which is called in your constructor.
您已经在构造函数中调用的Check()方法中处理表单。
The show method is then executed, and at this stage, you object is already disposed.
然后执行show方法,在此阶段,您已经处理了对象。
Memory in .net is freed automatically, why do you want to call dispose explicitly?
.net中的内存是自动释放的,为什么要显式调用dispose?
#5
0
Disposing the form is different from exiting the application. Try Application.Exit().
处理表单与退出应用程序不同。尝试Application.Exit()。