0

I have little problem with stopping infinite Pinging.

enter image description here

If you see in picture I ping IP 127.0.0.1 it has infinite ping ( -t ). And I want do that when I Click Stop! button then it stops pinging.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Management;

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 (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();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

Best wishes

KLDesigns,

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
KLDesigns
  • 21
  • 1
  • 4

1 Answers1

0

The simplest thing that might work in your current code is to get hold of your Process instance in the DataReceived event and cancel the process there (or send a Ctrl+C on the stdin).

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.Kill();
         // or more gently, send a ctrl+C
         // http://stackoverflow.com/a/285041/578411
         proc.StandardInput.Close();
    }
    if (e.Data != null)
    {
        string newLine = e.Data.Trim() + Environment.NewLine;
        MethodInvoker append = () => richTextBox1.Text += newLine;
        richTextBox1.BeginInvoke(append);
     }
}

You can set the boolean stop in your click event handler:

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

Keep in mind that if you want to keep control over object instances you create consider keeping a reference to them. As you create the Process inside the thread your form can't reach it any more. If you would have made the proc an member of your form you could have called proc.StandardInput.Close() from your click event.

rene
  • 41,474
  • 78
  • 114
  • 152