2011-08-29 56 views
0

您能否告訴我爲什麼我在運行此代碼時遇到運行時異常?WaitForExit()運行時異常。 (等待cmd進程完成)

Sub BtnNowClick(sender As Object, e As EventArgs) 

    Dim myProcess As New Process 
    Dim processFile As String = dlgFolder.SelectedPath 
    Dim pyLocationDel As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py") 

    Directory.SetCurrentDirectory(dlgFolder.SelectedPath) 

    myProcess.Start(pyLocationDel) 
    myProcess.WaitForExit()  
    System.IO.File.Delete(pyLocationDel) 


End Sub 

基本上,我想要的是檢測一個過程完成並刪除一個特定的文件,如果過程完成。

System.NullReferenceException: Object reference not set to an instance of an object. 
    at lgaPDF.MainForm.BtnNowClick(Object sender, EventArgs e) in C:\Documents and Settings\student3\My Documents\SharpDevelop Projects\lgaPDF\lgaPDF\MainForm.vb:line 184 
    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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at lgaPDF.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 

回答

0

因爲你從來沒有創建任何Process例如:

當試圖執行的代碼我得到這個錯誤。

您可以使用構造在聲明varaible:

Dim myProcess As New Process() 

設有獨立的構造函數調用:

Dim myProcess As Process 
myProcess = New Process() 
+0

謝謝你的提示,但是,我嘗試過,但我得到一個新錯誤'System.InvalidOperationException:沒有進程與此對象關聯。' – Brian

+0

我更新了問題中的代碼。 – Brian

+0

你不能像這樣垃圾人的答案。開始一個新的問題。 –