【发布时间】:2015-03-04 14:40:11
【问题描述】:
我写了一个运动检测winform c#桌面应用。
运动帧以单独的 jpeg 格式保存到我的硬盘中。
我使用 4 台摄像机进行录制。这由变量表示:
camIndex
每个 jpeg 都在一个文件结构下:
c:\年\月\日\时\分
以确保目录不会在每个目录中获取太多文件。
目的是让我的应用 24/7 全天候运行。应用可能会因系统重启或用户选择暂时关闭等原因而停止。
目前我有一个计时器,每 5 分钟运行一次,以删除超过 24 小时的文件。
我发现我的以下代码是内存密集型的,并且在几天的时间里 explorer.exe 已经在 RAM 内存中攀升。
我的运动应用程序需要一直打开,因此低内存占用对于这种“归档”至关重要...
以下代码很长,在我看来效率极低。有没有更好的方法可以实现我的目标?
我使用这个代码:
List<string> catDirs = Directory.EnumerateDirectories(Shared.MOTION_DIRECTORY, "*", SearchOption.TopDirectoryOnly).ToList();
for (int index = 0; index < catDirs.Count; index++)
{
for (int camIndex = 0; camIndex < 4; camIndex++)
{
if (Directory.Exists(catDirs[index] + "\\Catalogues\\" + camIndex.ToString()))
{
List<string> years = GetDirectoryList(catDirs[index] + "\\Catalogues\\" + camIndex.ToString(), true);
if (years.Count == 0)
{
Directory.Delete(catDirs[index]);
}
for (int yearIndex = 0; yearIndex < years.Count; yearIndex++)
{
DirectoryInfo diYear = new DirectoryInfo(years[yearIndex]);
List<string> months = GetDirectoryList(years[yearIndex], true);
if (months.Count == 0)
{
Directory.Delete(years[yearIndex]);
}
for (int monthIndex = 0; monthIndex < months.Count; monthIndex++)
{
DirectoryInfo diMonth = new DirectoryInfo(months[monthIndex]);
List<string> days = GetDirectoryList(months[monthIndex], true);
if (days.Count == 0)
{
Directory.Delete(months[monthIndex]);
}
for (int dayIndex = 0; dayIndex < days.Count; dayIndex++)
{
DirectoryInfo diDay = new DirectoryInfo(days[dayIndex]);
List<string> hours = GetDirectoryList(days[dayIndex], true);
if (hours.Count == 0)
{
Directory.Delete(days[dayIndex]);
}
for (int hourIndex = 0; hourIndex < hours.Count; hourIndex++)
{
DirectoryInfo diHour = new DirectoryInfo(hours[hourIndex]);
List<string> mins = GetDirectoryList(hours[hourIndex], false);
if (mins.Count == 0)
{
Directory.Delete(hours[hourIndex]);
}
for (int minIndex = 0; minIndex < mins.Count; minIndex++)
{
bool deleteMe = false;
DirectoryInfo diMin = new DirectoryInfo(mins[minIndex]);
DateTime foundTS = new DateTime(Convert.ToInt16(diYear.Name), Convert.ToInt16(diMonth.Name), Convert.ToInt16(diDay.Name),
Convert.ToInt16(diHour.Name), Convert.ToInt16(diHour.Name), 00);
double minutesElapsed = upToDateDate.Subtract(foundTS).TotalMinutes;
if (minutesElapsed > diff)
{
deleteMe = true;
}
if (deleteMe)
{
Directory.Delete(mins[minIndex], true);
}
}
}
}
}
}
}
}
【问题讨论】:
-
这是一个 很多 嵌套的
for循环!如果您准确解释代码的作用,您的问题会更清楚。目录结构是什么?文件是如何命名的?你用什么来确定他们的年龄? -
@AndrewSimpson - 给你:stackoverflow.com/questions/268132/…
-
在时间范围内看起来确实像嵌套目录。如果您每 24 小时冲洗一次所有东西,为什么需要它?如果您不必像那样循环,可以将代码简化为 2-3 行。
-
@AndrewSimpson:当程序重新启动时,您可以简单地重建队列。只需扫描您的目录并重建队列。
-
不知道这是否有用,但您可以查看
File.GetCreationTime:msdn.microsoft.com/en-us/library/…