执行 WaitForExit() 时出现运行时错误
我正在尝试执行以下代码,但调试器在 myProcess.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
I'm trying to execute the following code, but the debugger is failing at myProcess.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
Basically, what I want, is to detect for a process to be finished and delete a specific file if the process is finished.
I'm getting this error when trying to execute the code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
myprocess 仅被声明,从未实例化
myprocess is only declared, never instantiated