2 Replies - 873 Views - Last Post: 12 May 2012 - 08:29 AM Rate Topic: -----

#1 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Help how to cancel command?

Posted 12 May 2012 - 01:48 AM

Hi,

I was hoping somebody out there could help me please, I have an application which uses threading, process and runs cmd.exe - the problem I have is that I cant cancel the command.

I have one textbox and two command buttonts, button 1 is run and button 2 is cancel - for this testing purpose only (my app wont be pinging but I'm doing this for testing only) I'm hoping to enter an I.P address in the textbox and then click on Run which starts a thread and the process, which so far is working. the coding I use to run the command is..


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("ping " & TextBox1.Text & " >C:\pingtest.txt" & 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()




Now comes to difficult part, whilst the command is running I want to click on cancel which kills the thread and process which I have done but I have hit a brick wall because even though my thread and process has been killed my command carried on running, what I would like is the moment the cancel button is clicked to kill the thread, process and the command. I have tried everything underneath the sun but can't resolve this problem, can anyone help please?

Many thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Help how to cancel command?

#2 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Help how to cancel command?

Posted 12 May 2012 - 02:19 AM

Even after killing cmd.exe the command is still outputting to the textfile, the only realistic way I see canceling a command in cmd.exe is Control C but not sure how I could implement this in my application?
Was This Post Helpful? 0
  • +
  • -

#3 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Help how to cancel command?

Posted 12 May 2012 - 08:29 AM

Ahh cant believe what the problem was, even though the cmd process was killed ping.exe was still running, so killing this process has actually worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1