2016-07-13 2 views
0

無限のPingを止めることに少し問題があります。C#Ping -tボタンによる停止

enter image description here

あなたは画像IのpingのIP 127.0.0.1に表示されている場合には、無限のping(-t)を持っています。 そして私はクリックして停止するとしたい!ボタンを押すと、pingが停止します。あなたの現在のコードに働くかもしれない

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) 
     { 

     } 
    } 
} 

お祈り申し上げます

KLDesigns、

+1

これで、あなたのpingコマンドプロセスの停止に関して何をしようとしましたか – BugFinder

答えて

0

最も簡単な方法は、DataReceivedイベントにごProcessインスタンスのホールドを取得し、そこに処理をキャンセル(または送信することですstdinではCtrl + C)。

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); 
    } 
} 

あなたのクリックイベントハンドラでブールstopを設定することができます。

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

あなたがオブジェクトインスタンスに対する制御を維持したい場合は、それらへの参照を保存することを考え作成することに注意してください。スレッド内にProcessを作成すると、フォームはそれ以上アクセスできなくなります。 procをフォームのメンバーにした場合は、クリックイベントからproc.StandardInput.Close()を呼び出すことができます。

関連する問題