gpt4 book ai didi

c# - 从文件系统中删除文件时线程被中止错误

转载 作者:行者123 更新时间:2023-11-30 18:28:21 24 4
gpt4 key购买 nike

我在尝试从某个目录中删除一些临时文件时收到错误消息“Thread was being aborted”。我认为这个问题特定于下面的代码,因为在代码运行时应用程序池没有被回收(据我观察)。如果需要 3-5 秒,下面的代码将成功运行。但是当文件数量较多时,我会在 12 或 15 秒后收到错误消息。我假设它不是全局超时等,因为还有其他线程比这个线程花费更多的时间,但仍然没有被中止。会不会和I/O操作有关?这是一段代码:

DateTime now = DateTime.Now;
IEnumerable<string> directories = Directory.EnumerateDirectories(_pathOfTempFiles);
directories= directories.Concat(new[] { _pathOfTempFiles });
foreach (string dirPath in directories)
{

DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.EnumerateFiles())
{
if (now.Subtract(fi.CreationTime).TotalHours >= 2 && !fi.Name.ToLower().Contains("web.config") && !fi.Name.ToLower().Contains("web.confıg"))
{
try
{
fi.Delete();
}
catch (Exception ex)
{
_messages.Add("Exception : " + ex.Message + " ~ " + fi.Name);

}
}
}
}

最佳答案

一个大胆的猜测。不确定它没有被回收...你是deleting the files哪个触发应用程序池回收设计?询问“因为这可能会导致线程中止……在 2-3 秒内您可能会逃脱 - 应用程序无论如何都会重新启动,但您不会看到线程中止。在错误的地方进行大量/长时间的删除会触发一系列回收,从而更有可能出现异常。

此外:有许多设置(如不活动超时等)可能会使您的应用程序意外退出游戏。确保你没有击中那个。

关于c# - 从文件系统中删除文件时线程被中止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25161564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com