-1

I have problem with process stopping.

I tried one code but its killing programm not stopping process.

Program Form Design

Example:

I try to ping 127.0.0.1 and its starting infinite ping ( -t ) and i need to stop process, but its just killing it.

If i click "Stop!" then program stops and closed.

My code:

namespace PingProgramm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            th = new Thread(thread1);
            th.Start();
        }

        public void thread1()
        {
            try
            {
                string command = "/c ping -t " + textBox1.Text;
                ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
                Process proc = new Process();
                proc.StartInfo = procStartInfo;
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardInput = true;
                procStartInfo.RedirectStandardError = true;
                procStartInfo.UseShellExecute = false;
                procStartInfo.CreateNoWindow = true;
                proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.Start();
                proc.BeginOutputReadLine();
                proc.WaitForExit();
            }
            catch (Exception)
            {
                //if an error occurs with in the try block, it will handled here.
            }
        }
        void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (stop)
            {
                // sender is our process instance
                // so we can cast that safely here
                var proc = (Process)sender;
                // brutally kill it
                proc.Pause();
                // or more gently, send a ctrl+C
                // http://stackoverflow.com/a/285041/578411
                proc.StandardInput.Pause();
            }
            if (e.Data != null)
            {
                string newLine = e.Data.Trim() + Environment.NewLine;
                MethodInvoker append = () => richTextBox1.Text += newLine;
                richTextBox1.BeginInvoke(append);
            }
        }
        bool firstTime = true;
        private void textBox1_Click(object sender, EventArgs e)
        {
            if (firstTime)
            {
                firstTime = false;
                textBox1.Clear();
            }
        }

        bool stop = false;
        private void button2_Click(object sender, EventArgs e)
        {
            stop = true;
        }
    }
}

Best wishes,

KLDesigns

KLDesigns
  • 21
  • 1
  • 4
  • Is `Pause()` a Process extension method you wrote, and if so can we see it? I don't see it in the [`Process`](https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx) class. Also I wonder if your program is crashing because you are screwing around with the Process inside the OutputReceived event handler, which seems like a bad idea for some reason. I'm also lost as to what the `Pause()` here is: `proc.StandardInput.Pause();`. – Quantic Jul 13 '16 at 21:11
  • So... you have a comment above your line `proc.Pause()` that says, "brutally kill it". It would appear to be doing exactly what the comment implies. Also, from the linked SO post, it says to use `proc.StandardInput.Close()` not `Pause()`. You probably have at least one exception being thrown... Please do some research into how to debug. – Heretic Monkey Jul 13 '16 at 21:15
  • You have an event registered with '+='. Stopping the process won't immediately stop the ping responses from occurring. It may take 5 to 10 seconds before the responses stop. You probably want to un-register the OutputDataReceived event before stopping the process so you don't get any exceptions. – jdweng Jul 13 '16 at 22:00

1 Answers1

1

I copied and pasted your code into VS and got it to work using the following changes:

First, comment out or delete "proc.WaitForExit();" in the thread1 definition... this is not the place where you are waiting for the thread to exit, and it seems to be causing problems.

Next, change proc_OutputDataReceived to the following:

void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (stop)
    {
        var proc = (Process)sender;

        stop = false; // allows you to spawn a new thread after stopping the first
        proc.SynchronizingObject = this; // puts the form in charge of async communication
        proc.Kill(); // terminates the thread
        proc.WaitForExit(); // thread is killed asynchronously, so this goes here...

    }
    if (e.Data != null)
    {
        string newLine = e.Data.Trim() + Environment.NewLine;
        MethodInvoker append = () => richTextBox1.Text += newLine;
        richTextBox1.BeginInvoke(append);
    }
}

With those changes, you can stop the ping request and then restart a new one.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38