When deleting files from the file system, the stream was interrupted

I am getting the "Thread was aborted" error when I try to delete some temporary files from a specific directory. I think this problem is specific to the code below, because the application pool is not being recycled while the code is running (as far as I noticed). The part of the code below works successfully if it takes 3-5 seconds. But when there are more files, I get an error after 12 or 15 seconds. I am assuming it is not a global timeout etc., because there are other threads taking much longer than this and still not interrupted. Could this be related to I / O operations? This is a piece of code:

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);

            }
        }
    }
}

      

+3


source to share


1 answer


Wild guess. Not sure if it won't recycle ... Are you deleting files that run the app pool utility by design? Ask, because it can lead to interrupted threads ... After 2-3 seconds you can get away from this - the app will be overwritten anyway, but you won't see a draw. Massive / long deletions in the wrong places can cause a series of repeated loops making exceptions more likely.



Also: There are many settings (like idle timeout, etc.) that can unexpectedly take your app out of the game. Make sure you haven't.

0


source







All Articles