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

时间:2022-05-02 15:41:12

I get the error "Thread was being aborted" while trying to delete some temporary files from a certain directory. I think this problem is specific to the code below, because application pool is not recycled while the code is running (as far as I observed). The piece of code below runs successfully if it takes 3-5 seconds. But when the count of files are bigger, I get the error after 12 or 15 seconds. I assume its not a global timeout etc because there are other threads which take much more time than this one, and still not get aborted. Can it be related to I/O operation? This is the piece of code:

我在尝试从某个目录中删除一些临时文件时收到错误“Thread is 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);

            }
        }
    }
}

1 个解决方案

#1


0  

A wild guess. Not sure it's not getting recycled... Are you deleting the files which trigger App Pool recycling by design? Asking 'cause this can lead to aborted threads... In 2-3 seconds you might get away with that -- the app will be relaunched anyway, but you won't see a thread abort. Massive/lengthy deletions in the wrong places can trigger series of recycles, making the exceptions more likely.

一个疯狂的猜测。不确定它没有被回收......你是否正在删除触发应用程序池回收的文件?询问'因为这会导致线程中止...在2-3秒内您可能会侥幸逃脱 - 应用程序将会重新启动,但您不会看到线程中止。在错误的地方进行大规模/冗长的删除可以触发一系列的回收,从而更有可能发生异常。

Also: there are numerous settings (like inactivity timeout, etc), which might put your app out of the game unexpectedly. Make sure you don't hit that.

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

#1


0  

A wild guess. Not sure it's not getting recycled... Are you deleting the files which trigger App Pool recycling by design? Asking 'cause this can lead to aborted threads... In 2-3 seconds you might get away with that -- the app will be relaunched anyway, but you won't see a thread abort. Massive/lengthy deletions in the wrong places can trigger series of recycles, making the exceptions more likely.

一个疯狂的猜测。不确定它没有被回收......你是否正在删除触发应用程序池回收的文件?询问'因为这会导致线程中止...在2-3秒内您可能会侥幸逃脱 - 应用程序将会重新启动,但您不会看到线程中止。在错误的地方进行大规模/冗长的删除可以触发一系列的回收,从而更有可能发生异常。

Also: there are numerous settings (like inactivity timeout, etc), which might put your app out of the game unexpectedly. Make sure you don't hit that.

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