[RESOLVED] Help on my Process coding
Hi,
I have some coding which just runs cmd commands.
Code:
Private Sub RunCMD()
Dim myProcess As Process = New Process
Dim s As String
myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
Dim sErr As System.IO.StreamReader = myProcess.StandardError
sIn.AutoFlush = True
sIn.Write("Insert Command Here" & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
s = sOut.ReadToEnd()
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim processThread As New Threading.Thread(AddressOf RunCMD)
processThread.IsBackground = True
processThread.Start()
End Sub
Now the code works perfectly fine but would it be possible to make the coding shorter or is there any extra coding which I can remove?
Re: Help on my Process coding
Not really. What Id do is make a function to create the process object and set its StartInfo before returning the reference. This doesn't actually reduce the amount of code but it would make RunCMD smaller.
Re: Help on my Process coding
thanks, one more thing if I wanted to add a cancel button which will kill any process and any cmd what would be the best way?
If possible can you also give me an example?
I was thinking.
Code:
Dim processThread As New Threading.Thread(AddressOf RunCMD)
processThread.IsBackground = True
processThread.Abort
but this does not kill the command.
Re: Help on my Process coding
What I'm suggesting is really simple. Basically what I meant is that you can split that procedure into two procedures. One to set up the process object and the other to do that other stuff you wrote:-
vbnet Code:
'
Public Function CreateCMDProcess() As Process
Dim myProcess As Process = New Process
myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
Return myProcess
End Function
You simply call this procedure to return the process thereby reducing the amount of code in RunCMD
Re: Help on my Process coding
ok many thanks, what are your thoughts about the cancel button? is that possible?
Re: Help on my Process coding
Your thread doesn't run for long so I don't see a need for a cancel in this case. What are you trying to do there any way ?
Re: Help on my Process coding
well some commands that I run will be collecting data, they can run for up to 1 hour at a time so I would need a way of canceling the process if possible.
Re: Help on my Process coding
Then you should widen the scope of myProcess because as it is now, it will go out of scope leaving nothing to reference the process. Also, each process in Windows has its own set of threads so executing RunCMD on a different thread is a bit redundant and you don't seem to be waiting for the process to exit in that thread.