WaitForExit()运行时异常。 (等待cmd进程完成) [英] WaitForExit() Runtime Exception. (Waiting for cmd process to finish)

查看:295
本文介绍了WaitForExit()运行时异常。 (等待cmd进程完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Sub BtnNowClick(sender作为对象,作为EventArgs)

Dim myProcess作为新进程
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()
系统。 IO.File.Delete(pyLocationDel)


End Sub

基本上,我想要的是检测一个进程是否完成,如果进程完成,删除一个特定的文件。



以执行代码:

  System.NullReferenceException:对象引用未设置为对象的实例。 
at lgaPDF.MainForm.BtnNowClick(Object sender,EventArgs e)in C:\Documents and Settings\student3\My Documents \SharpDevelop Projects\lgaPDF\lgaPDF\MainForm.vb:line 184
在System.Windows.Forms.Control.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent)
在System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons按钮,Int32点击)
在System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
在System.Windows.Forms.Button.WndProc(Message& m)
在System.Windows.Forms.Control.ControlNativeWindow .OnMessage(Message& m)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData)
在System.Windows.Forms.Application .ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在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


方案

因为你从不创建任何 Process 实例。



你声明的变量:

  Dim myProcess作为新进程()

使用单独的构造函数调用:

  b $ b myProcess = New Process()


Can you kindly tell me why I am getting a runtime exception when trying to run this code?

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

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

解决方案

Because you never create any Process instance.

You can use the constructor when you declare the varaible:

Dim myProcess As New Process()

With a separate constructor call:

Dim myProcess As Process
myProcess = New Process()

这篇关于WaitForExit()运行时异常。 (等待cmd进程完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆