运行时错误执行WaitForExit()
问题描述:
我试图执行下面的代码,但调试失败在myProcess.WaitForExit()
运行时错误执行WaitForExit()
Sub BtnNowClick(sender As Object, e As EventArgs)
Dim myProcess As Process
Dim processFile As String = dlgFolder.SelectedPath
Dim pyLocationDel As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
Directory.SetCurrentDirectory(dlgFolder.SelectedPath)
myProcess.Start("pdfmerge.py")
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
答
试试这个:
Sub BtnNowClick(sender As Object, e As EventArgs)
Dim processFile As String = dlgFolder.SelectedPath
Dim pyLocationDel As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
Directory.SetCurrentDirectory(dlgFolder.SelectedPath)
Dim myProcess As Process = Process.Start("pdfmerge.py")
myProcess.WaitForExit()
System.IO.File.Delete(pyLocationDel)
End Sub
+0
那工作,谢谢! – Brian
答
Dim myProcess As NEW Process
myprocess只宣布,从不实例
+0
当我尝试我得到这个错误信息:'System.InvalidOperationException:没有进程与此对象关联.' – Brian
这个问题有已被回答。如果您对更改后的代码有任何问题,则必须发布更改后的代码。 – Guffa
@Guffa嘿,我已经更新了代码并发布了新的错误消息,但另一位用户告诉我发布有关新问题的新问题! – Brian
您再次发布了相同的代码,而不是更新后的代码。 – Guffa