【问题标题】:Runtime Error when executing WaitForExit()执行 WaitForExit() 时出现运行时错误
【发布时间】:2011-08-29 09:25:17
【问题描述】:

我正在尝试执行以下代码,但调试器在 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

【问题讨论】:

  • 这个问题已经回答了。如果您对更改后的代码有任何问题,您必须发布更改后的代码。
  • @Guffa 嘿,我已经更新了代码并发布了新的错误消息,但另一位用户告诉我发布关于新问题的新问题!
  • 您再次发布了相同的代码,而不是更新后的代码。
  • @Guffa 我更新了一些代码和错误消息,正如我告诉你的,我被要求开始一个新问题,因为它与原始问题无关。
  • 是的,新问题应该包含更新后的代码,而不是原始问题的副本。正如您在下面看到的,您只会得到与原始问题相同的答案。

标签: vb.net process cmd delete-file waitforexit


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:
    Dim myProcess As NEW Process 
    

    myprocess 只被声明,从不实例化

    【讨论】:

    • 当我尝试收到此错误消息时:System.InvalidOperationException: No process is associated with this object.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多