0
我正在使用Tshark開始使用CommandLine進行捕獲,使用此進程的輸出我正在計算接收到的數據包數量:我的StreamReader讀取進程的輸出並在此輸出內部解析數據包號碼,以便在我的表單上顯示此號碼。 我的問題是我殺了進程之後,我顯示的數據包數量與文件內的實際數據包(pcap文件)之間存在差異,所以我的問題是如果有殺死進程的方法,但要等到所有處理輸出完成。獲取所有進程輸出,即使進程被終止後
public class Tshark
{
public int _interfaceNumber;
public string _pcapPath;
public int _test;
public int _packetsCount;
public string _packet;
public delegate void dlgPackProgress(int progress);
public event dlgPackProgress evePacketProgress;
public Tshark(int interfaceNumber, string pcapPath)
{
_interfaceNumber = interfaceNumber;
_pcapPath = pcapPath;
}
public void startTheCapture()
{
Process _tsharkProcess1 = new Process();
_tsharkProcess1.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
_tsharkProcess1.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -V -x -w " + _pcapPath);
_tsharkProcess1.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
_tsharkProcess1.StartInfo.RedirectStandardOutput = true;
_tsharkProcess1.StartInfo.UseShellExecute = false;
_tsharkProcess1.StartInfo.CreateNoWindow = true;
_tsharkProcess1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_tsharkProcess1.Start();
Thread.Sleep(2000);
DateTime lastUpdate = DateTime.MinValue;
StreamReader myStreamReader = _tsharkProcess1.StandardOutput;
while (!myStreamReader.EndOfStream)
{
_packet = myStreamReader.ReadLine();
if (_packet.StartsWith(" Frame Number:"))
{
string[] arr = _packet.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
_test = int.Parse(arr[2]);
_packetsCount++;
}
if ((DateTime.Now - lastUpdate).TotalMilliseconds > 1000)
{
lastUpdate = DateTime.Now;
OnPacketProgress(_packetsCount++);
}
}
_tsharkProcess1.WaitForExit();
}
private void OnPacketProgress(int packet)
{
var handler = evePacketProgress;
if (handler != null)
{
handler(packet);
}
}
public void killProcess()
{
foreach (Process prc in System.Diagnostics.Process.GetProcessesByName("tshark"))
{
prc.Kill();
prc.WaitForExit();
}
}
private void process_OutputDataReceived(object sender, DataReceivedEventArgs arg)
{
string srt = arg.Data; //arg.Data contains the output data from the process...
}
}
我該怎麼做? – falukky
不知道。你有控制Tshark嗎?不知道那是什麼。 –
這是一個控制檯應用程序的權利。在控制檯你可以做任何比CTRL + C更多的事情嗎?如果沒有,那麼GenerateConsoleCtrlEvent將以編程方式執行相同的操作。 –