2013-12-10 53 views
2

的MainForm之后ObjectDisposed以外的所有上面的代码运行入门关闭形式

Public Partial Class Form1 
    Public Sub New() 
     ' The Me.InitializeComponent call is required for Windows Forms designer support. 
     Me.InitializeComponent() 
     Dim filePath As String 
     filePath = System.IO.Path.Combine(MainForm.folderpath,"logininfo.cfg") 
     Dim adptrname As String 
     adptrname=MainForm.adptername.Trim 
     MsgBox(adptrname.Length) 
     Dim args As String 

     args="netsh int ipv4 set address name=""" & adptrname & """ source=dhcp" 
     Dim proc As New System.Diagnostics.Process()   
     proc.StartInfo.FileName = "netsh" 
     proc.StartInfo.Verb="RunAs" 
     proc.StartInfo.CreateNoWindow = true 
     proc.StartInfo.RedirectStandardOutput = True 
     proc.StartInfo.RedirectStandardError = True 
     proc.StartInfo.Arguments = args 
     proc.StartInfo.UseShellExecute = False 
     proc.Start() 


     My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardOutput.ReadToEnd(), True) 
     My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardError.ReadToEnd(), True) 


     proc.WaitForExit() 

     MsgBox(args) 

     MsgBox(adptrname) 
     Me.Close 

    End Sub 
End Class 

后,我得到了ObjectDisposed在mainform中的Form1.show()行异常我无法理解哪个处置对象被调用在这里。

System.ObjectDisposedException: Cannot access a disposed object. 
    at System.Windows.Forms.Control.CreateHandle() 
    at System.Windows.Forms.Form.CreateHandle() 
    at System.Windows.Forms.Control.get_Handle() 
    at System.Windows.Forms.Control.SetVisibleCore(Boolean value) 
    at System.Windows.Forms.Form.SetVisibleCore(Boolean value) 
    at System.Windows.Forms.Control.Show() 
    at SIBMConnect.MainForm.Button3Click(Object sender, EventArgs e) in C:\Users\JONAH\Documents\SharpDevelop Projects\SIBMConnect\SIBMConnect\MainForm.vb:line 92 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(ApplicationContext context) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at SIBMConnect.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 

请帮

+0

为什么这个标记的VBA? – 2013-12-10 10:45:27

+0

你不应该写Form1.show()这样的代码。使用'New Form1()。Show()' –

+0

你在哪一行得到异常?这个例外的完整细节应该使这个非常清晰...... –

回答

1

您在Form1构造函数(它将调用Dispose)调用Close(),所以当你创建的Form1一个实例,这将已经在“配置”状态(你可以通过阅读IsDisposed属性来检查它)。

只是不要在构造函数中调用Close。事实上,除了初始化对象之外,不要在构造函数中做任何事情。

如果你不想展示它,你为什么要用Form

+0

做这样的事情有完全有效的理由。例如,您可能会重载构造函数以获取发送给main的命令行参数,并且导致应用程序需要终止的错误。 – Aelphaeis