Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何等待进程在磁盘上创建文件,以避免检查此文件时出现空值_C# - Fatal编程技术网

C# 如何等待进程在磁盘上创建文件,以避免检查此文件时出现空值

C# 如何等待进程在磁盘上创建文件,以避免检查此文件时出现空值,c#,C#,我有应用程序如何打开Tshark进程并开始捕获数据包,这个进程在磁盘上创建pcap文件,从我的主窗体我检查这个类属性并更新我的GUI 最近我添加了一个选项,即检查磁盘上的文件大小和此属性,我的问题是,在我启动函数后,表示文件大小的属性尝试检查该文件,但如果进程没有创建该文件,则该属性为空,并且我的应用程序崩溃,因此我添加了Thread.Sleep,现在它正在工作,但我想知道是否有更好的方法来执行该操作 这是我的类,有一个开始捕获的函数,我说的属性是_myFile和Thread.Sleep,我想更

我有应用程序如何打开Tshark进程并开始捕获数据包,这个进程在磁盘上创建pcap文件,从我的主窗体我检查这个类属性并更新我的GUI

最近我添加了一个选项,即检查磁盘上的文件大小和此属性,我的问题是,在我启动函数后,表示文件大小的属性尝试检查该文件,但如果进程没有创建该文件,则该属性为空,并且我的应用程序崩溃,因此我添加了Thread.Sleep,现在它正在工作,但我想知道是否有更好的方法来执行该操作

这是我的类,有一个开始捕获的函数,我说的属性是_myFile和Thread.Sleep,我想更改的是在我的tshark.start()之后

公共类Tshark2
{
#地区级成员
公共字符串(u tshark);;
公共字符串\u文件路径;
公开名单(public List);;
公共流程StartInfo\u流程;
公共对象;
公共int_接口枚举器;
公共字符串_pcapPath;
公共字符串状态;
公共INTU接收包裹;
公共国际包裹;
公共字符串包;
公共双比特秒;
公共双包每秒;
公共十进制_packetLimitSize;
公共日期时间\u lastTimestamp;
包装装置(PacketDevice);;
公共委托无效dlgPackProgress(int progress);
公共事件dlgPackProgress evePacketProgress;
公共目录信息(public DirectoryInfo);
公共文件信息(myFile);;
公共文件信息\u文件信息;
公共文件信息[]_目录;
公共长文件大小;
公共无效开始许可证()
{
_状态=“正在收听…”;
ThreadStart tStarter=委托{openAdapterForStatistics(_设备);};
螺纹=新螺纹(t开始);
thread.IsBackground=true;
thread.Start();
进程tshark=新进程();
tshark.StartInfo.FileName=\u tshark;
tshark.StartInfo.Arguments=string.Format(“-i”+_interfaceEnumber+”-V-x-s”+_packetLimitSize+“-w”+_pcapPath);
tshark.StartInfo.RedirectStandardOutput=true;
tshark.StartInfo.UseShellExecute=false;
tshark.StartInfo.CreateNoWindow=true;
tshark.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
tshark.Start();
睡眠(1000);
DateTime lastUpdate=DateTime.MinValue;
StreamReader myStreamReader=tshark.StandardOutput;
_fileInfo=新的fileInfo(_pcapPath);
字符串directoryName=\u fileInfo.directoryName;
_directoryInfo=新的directoryInfo(directoryName);
_dirs=_directoryInfo.GetFiles();
_myFile=_dirs.FirstOrDefault(f=>f.Name.Equals(_fileInfo.Name));
而(!myStreamReader.EndOfStream)
{
_packet=myStreamReader.ReadLine();
if(_packet.StartsWith(“帧号:”))
{
string[]arr=_packet.Split(默认值(char[]),StringSplitOptions.RemoveEmptyEntries;
_receivespackes=int.Parse(arr[2]);
_packetscont++;
}
如果((DateTime.Now-lastUpdate).totalmillizes>1000)
{
lastUpdate=DateTime.Now;
OnPacketProgress(_packetscont++);
}
}
tshark.WaitForExit();
}
}

尝试Process.WaitForExit,而不是固定的睡眠时间。看

编辑: 如果您试图利用流程的输出,那么WaitForExit应该在读取您正在生成的流程的输出之前执行。在这种情况下,WaitForExit将取代Thread.Sleep。如果要在写入文件时监视文件大小,请等待文件创建,然后使用计时器检查文件的写入进度。请参见下面的示例

for (int i = 20 /* seconds till exception */; i > 0; i--)
{
    if (File.Exists(_pcapPath))
        break;
    else if (i == 1)
        throw new Exception("File was not created within 20 seconds.");
    else
        Thread.Sleep(1000);
}

while (!tshark.WaitForExit(1000 /* file size update interval */))
{
    var fileInfo = new FileInfo(_pcapPath);
    Console.WriteLine("File has grown to {0} bytes", fileInfo.Length);
}

Console.WriteLine("Complete");

使用诸如为什么要使用Tshark之类的东西要容易得多?您知道不使用外部进程就可以完成吗?我在应用程序中使用pcapdot.net(捕获只是一个次要功能),但使用Wireshark进程可以让我有更多的选择。我使用tshark.WaitForExit();在课程的最后看到我的代码,但它没有帮助
for (int i = 20 /* seconds till exception */; i > 0; i--)
{
    if (File.Exists(_pcapPath))
        break;
    else if (i == 1)
        throw new Exception("File was not created within 20 seconds.");
    else
        Thread.Sleep(1000);
}

while (!tshark.WaitForExit(1000 /* file size update interval */))
{
    var fileInfo = new FileInfo(_pcapPath);
    Console.WriteLine("File has grown to {0} bytes", fileInfo.Length);
}

Console.WriteLine("Complete");