ArxOne / FTP

Simple FTP client
MIT License
37 stars 15 forks source link

ThreadInterruptedException' in mscorlib.dll. Doesnt crash but dont know why it happens. #36

Closed A-Force closed 7 years ago

A-Force commented 7 years ago

Hello! Thank you for this FTP library. It seems well written and easy to use.

I have a c# program that occasionally retrieves a number of small files from another device using ftp. The program works, but an exception is thrown every time and does not get caught in my try catch block. "Exception thrown: 'System.Threading.ThreadInterruptedException' in mscorlib.dll" The exception does not cause the program to crash but i wonder why it is happening. Perhaps you can help?

Here is my code:

private bool FTPGetLogfilesFromPLC()
        {
            // Initiate ftp variables and file paths
            Uri plcFtpUri = new Uri("ftp://" + PLC_IPAddress);
            NetworkCredential plcFtpCred = new NetworkCredential("user", "pass");
            FtpPath plcLogPath = new FtpPath("MEMCARD1");
            // Get the list of .csv files already present in the PC harddrive
            FileInfo[] localFiles = new DirectoryInfo(LocalLogPath).GetFiles("*.csv");

            FtpClientParameters par = new FtpClientParameters();
            par.ConnectTimeout = TimeSpan.FromMinutes(1);
            try
            {
                using (var ftpClient = new FtpClient(plcFtpUri, plcFtpCred, par))
                {
                    // Using FTP, get the list of files existing on the PLC side
                    IEnumerable<FtpEntry> plcFiles = ftpClient.MlsdEntries(plcLogPath);

                    foreach (FtpEntry plcFile in plcFiles)
                    {
                        int localFileIndex = -1;
                        bool download = false;

                        // Step through all .csv files on the plc side, download if the size differs or if they dont exist locally.
                        if (plcFile.Size != null && plcFile.Name.Contains(".csv"))
                        {
                            //Console.WriteLine("File found in PLC: " + plcFile.Name);
                            localFileIndex = Array.FindIndex(localFiles, localFile => localFile.Name.Equals(plcFile.Name));
                            if (localFileIndex != -1)
                            {
                                //Console.WriteLine("The file exists locally.");
                                if (localFiles[localFileIndex].Length != plcFile.Size)
                                {
                                    Console.WriteLine("File: " + plcFile.Name + " has unseen data. Downloading.");
                                    download = true;
                                }
                                else
                                    download = false;
                            }
                            else
                            {
                                Console.WriteLine("File: " + plcFile.Name + " does not exist locally. Downloading.");
                                download = true;
                            }
                            if (download)
                                using (FileStream newLocalFile = File.Create(LocalLogPath + @"\" + plcFile.Name))
                                {
                                    using (Stream ftpDLstream = ftpClient.Retr(plcFile.Path, FtpTransferMode.Binary))
                                        ftpDLstream.CopyTo(newLocalFile);
                                }
                        }
                    }
                }
                Console.WriteLine("Exiting FTP function.");
                return true;
            }
            catch (Exception ex)
            {
                AddToEventLog("An exception occured. See log file for details.");
                Util.AppendToErrorLog(LogFiles.App, "Could not FTP to PLC:\n" + ex.ToString());
                return false;
            }
        }

And here is the output of the program: File: 2016-12-20.csv does not exist locally. Downloading. File: 2016-12-21.csv does not exist locally. Downloading. File: 2016-12-22.csv does not exist locally. Downloading. Exception thrown: 'System.Threading.ThreadInterruptedException' in mscorlib.dll The thread 0x3920 has exited with code 0 (0x0). Exiting FTP function.

As you can see, the exception is thrown while inside my ftp function but seemingly on an other thread as it is not caught in my try-catch block. All files are succesfully downloaded and my function returns "true".

picrap commented 7 years ago

Could you provide the full stack trace of the exception, including the inner exceptions, if any?

A-Force commented 7 years ago

I am unable to get the details of the exception, since it does not trigger my try-catch block. I tried changing exception settings to break on all exceptions, which caused a breakpoint but no detail was available except: System.Threading.ThreadInterruptedException occurred Message: Exception thrown: 'System.Threading.ThreadInterruptedException' in mscorlib.dll Additional information: Thread was interrupted from a waiting state.

It said something about viewing details in the "Disassembly" but frankly i dont know how to do that.

Thank you for your interest and quick response by the way.

picrap commented 7 years ago

When the exception is thrown, it generates a breakpoint. Can't you then just watch the exception being thrown? There should be at least one stack trace.

picrap commented 7 years ago

I think I understand. You see this exception in debugger display, but it is never caught and does not break the application, right? Then it comes from the FtpClient.Dispose() method, and you can safely ignore it.

A-Force commented 7 years ago

Yeah, i see it in the console output of the program. Both in debug and release mode, but it does not break the application unless i configure to break on all exceptions in the exception settings.

So i guess its not really a problem but i wondered if it could be solved somehow. Thanks for your help either way! And sorry for my late response. You can close this if you want.

picrap commented 7 years ago

So i guess its not really a problem but i wondered if it could be solved somehow.

This is not a problem at all, it is a regular way to interrupt a thread 😉