在我的应用程序中我打开Tshark进程并开始捕获并从我的主要表单我更新我的UI。 我的一个类属性是我的文件如何创建(pcap文件),在我的UI上我更新此文件长度正在进行中。 我的问题是,如果这个文件是新的,长度保持为零,但如果我的文件已经存在,我的UI显示我的长度正在进行(当然文件被覆盖,但我可以看到它在增长)。
public class Tshark
{
#region members
private Process _tsharkProcess = new Process();
private int _numberOfPackets;
private string _tshark;
private List<string> _list;
private ProcessStartInfo _process;
private int _interfaceNumber;
private string _pcapPath;
private string _status;
private double _bitsPerSecond;
private double _packetsPerSecond;
private decimal _packetLimitSize;
private DateTime _lastTimestamp;
private PacketDevice _device;
private FileInfo _file;
private bool _processRunning;
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();
_processRunning = true;
_tsharkProcess.BeginOutputReadLine();
_tsharkProcess.BeginErrorReadLine();
_tsharkProcess.WaitForExit();
}
public long getFileLength()
{
_file = new FileInfo(_pcapPath);
string directoryName = _file.DirectoryName;
DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);
FileInfo[] dirs = directoryInfo.GetFiles();
_file = dirs.FirstOrDefault(f => f.Name.Equals(_file.Name));
if (_file != null)
{
return _file.Length;
}
return 0;
}
public void refreshFile()
{
if (_file != null)
{
_file.Refresh();
}
}
}
从主表单我通过计时器更新我的UI:
private void tmrUpdateLbl_Tick(object sender, EventArgs e)
{
lblFileSizeTabSniffer2.Text = formatBytes(tshark.getFileLength());
tshark.refreshFile();
}
在dibugger我可以看到,如果文件是新的,_file始终为null,所以在getFileLength()方法中返回零。
答案 0 :(得分:0)
您需要每次(即getFileLength
)获取完整的FileInfo以获取最新结果。将相关代码从startCapturing
移至getFileLength
。