在我的應用程序即時通信開放Wireshark的過程,並開始捕捉packts,在UI我有開始按鈕誰開始拍攝和停止按鈕誰停止捕獲和我我是通過殺死我的wireshark進程來做到這一點的。 我的問題是,如果我靠近在捕獲的中途我的應用程序,但也不符合我的停止按鈕,我的Wireshark的過程中繼續運行,我該怎麼處理這種情況,並確保我的進程將關閉,如果我的應用程序崩潰或你身邊它捕捉如何確保我的過程,我開始將關閉我的應用程序崩潰的
public void startCapturing()
{
ThreadStart tStarter = delegate { openAdapterForStatistics(_device); };
Thread thread = new Thread(tStarter);
thread.IsBackground = true;
thread.Start();
ProcessStartInfo tsharkStartInfo = new ProcessStartInfo();
tsharkStartInfo.FileName = _tshark;
tsharkStartInfo.RedirectStandardOutput = true;
tsharkStartInfo.RedirectStandardError = true;
tsharkStartInfo.RedirectStandardInput = true;
tsharkStartInfo.UseShellExecute = false;
tsharkStartInfo.CreateNoWindow = true;
tsharkStartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -s " + _packetLimitSize + " -w " + _pcapPath);
_tsharkProcess.StartInfo = tsharkStartInfo;
_tsharkProcess.ErrorDataReceived += _cmdProcess_ErrorDataReceived;
_tsharkProcess.OutputDataReceived += tshark_OutputDataReceived;
_tsharkProcess.EnableRaisingEvents = true;
_tsharkProcess.Start();
_tsharkProcess.BeginOutputReadLine();
_tsharkProcess.BeginErrorReadLine();
FileInfo fileInfo = new FileInfo(_pcapPath);
string directoryName = fileInfo.DirectoryName;
DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);
FileInfo[] dirs = directoryInfo.GetFiles();
_file = dirs.FirstOrDefault(f => f.Name.Equals(fileInfo.Name));
_tsharkProcess.WaitForExit();
}
誰打開的過程中我的Wireshark類有開始捕獲方法和殺方法,以便在那裏把這個代碼? – user1710944
這真的取決於你的代碼的結構。我不知道你的代碼的結構,所以我不能肯定地說。如果你想確保Wireshark被關閉,無論拋出什麼異常,你都需要把這個'try-catch-finally'放在你控制的最高點。例如,如果它是一個控制檯應用程序,你應該把它放在Main方法中。如果它是一個GUI應用程序,則主窗體的OnLoad事件可能會起作用。 – jebar8
我更新我的代碼並添加我的開始捕獲方法(Tshark類的一部分) – user1710944