Advertisement
Guest User

CommandExecutor

a guest
Jul 16th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. Public Class CommandExecutor
  2. Implements IDisposable
  3.  
  4. Public Event OutputRead(ByVal output As String)
  5.  
  6. Private WithEvents _process As Process
  7.  
  8. Public Sub Execute(ByVal filePath As String, ByVal arguments As String)
  9. If _process IsNot Nothing Then
  10. Throw New Exception("Already watching process")
  11. End If
  12. _process = New Process()
  13. _process.StartInfo.FileName = filePath
  14. _process.StartInfo.Arguments = arguments
  15. _process.StartInfo.UseShellExecute = False
  16. _process.StartInfo.RedirectStandardOutput = True
  17. _process.Start()
  18. _process.BeginOutputReadLine()
  19. End Sub
  20.  
  21. Private Sub _process_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles _process.OutputDataReceived
  22.  
  23. If _process.HasExited Then
  24. _process.Dispose()
  25. _process = Nothing
  26. End If
  27. RaiseEvent OutputRead(e.Data)
  28. End Sub
  29.  
  30. Private disposedValue As Boolean = False
  31. Protected Overridable Sub Dispose(ByVal disposing As Boolean)
  32. If Not Me.disposedValue Then
  33. If disposing Then
  34. If _process IsNot Nothing Then
  35. _process.Kill()
  36. _process.Dispose()
  37. _process = Nothing
  38. End If
  39. End If
  40. End If
  41. Me.disposedValue = True
  42. End Sub
  43.  
  44. Public Sub Dispose() Implements IDisposable.Dispose
  45. Dispose(True)
  46. GC.SuppressFinalize(Me)
  47. End Sub
  48. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement