7

I have a class in a C# program (.Net 4.61) that uses Word and the Amyuni PDF suite to build a formatted PDF file. During the process, four temporary PDF files are created in the user's temp folder:

private string TempFolder = Path.GetTempPath();

When the process completes, I have the following cleanup method that runs to delete any temp files generated during the process:

private void EraseTempFiles()
{
    // For each temp file:
    foreach (string tempFile in TempFiles)
    {
        if (File.Exists(tempFile))
        {
            File.Delete(tempFile);
        }
    }
}

This works file on nearly all of the deployed Windows systems (all 64-bit Windows 7 systems), however on a few of them, when the EraseTempFiles method deletes the third of the temporary files, the application immediately exits. No exceptions are thrown (I've surrounded the File.Delete in a try-catch to determine this).

The files can be deleted using Windows Explorer no problem.

I've tried saving all temp files in a different folder and changing how they are named with no change in behavior. Running ProcDump to track the application has not caught anything, it simply reports:

[23:54:52] The process has exited.
[23:54:52] Dump count not reached.

I've also subscribed to the unhandled exception event:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

private void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
  System.Windows.Forms.MessageBox.Show(((Exception)e.ExceptionObject).Message);
  throw new NotImplementedException();
}

That handler is also never called.

I've never seen a program exit like this with no explanation. It seems that Windows itself might be killing the process due to some violation, but I can't tell what since all of the temp files are basically the same and it is able to delete the first two with no problem.

Anyone seen this type of behavior before?

Community
  • 1
  • 1
twitort
  • 313
  • 2
  • 11
  • 1
    Have you looked in your Windows Event Log? – Chris Dunaway Nov 30 '17 at 18:50
  • sounds like a `permissions` issue. wrap the Delete around a try catch and see if it tells you that and or a reason why it did not delete – MethodMan Nov 30 '17 at 18:57
  • This is my pure guess as to why and so you should take with a grain of salt. Windows is trying to access the same file while your deletion and since its system, it has priority over your code it manipulates it first. And your state is invalid while execution thus no exception. I would go with acquiring asystem global mutex over whole foreach loop. https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx This should not in any way slow down your application. – Tomasz Juszczak Nov 30 '17 at 18:58
  • 1
    I suspect Word or Amyuni PDF to still hold some handles for these files. Make sure the Word and Amyuni PDF objects are closed and disposed before deleting the files. Automating Office applications from another application has always been fragile. – Olivier Jacot-Descombes Nov 30 '17 at 19:03
  • MethodMan - I tried the try-catch as I mentioned in my post. The catch is never reached. No exception is raised. – twitort Nov 30 '17 at 19:08
  • Olivier Jacot-Descombes - both Word and the Amyuni object are closed before the temp files are deleted, but I can confirm that Amyuni's "Terminate" method releases all handles. Thanks. – twitort Nov 30 '17 at 19:12
  • Chris Dunaway - checked the event log and no alerts or errors logged when this is happening. – twitort Nov 30 '17 at 19:38
  • I am beginning to suspect Trend Micro might be killing the process. This is managed by a third party IT support company, so I need to track down the admin password to disable it to test. Maybe there's a white list that my application needs to be added to. – twitort Nov 30 '17 at 19:41

1 Answers1

11

After disabling Trend Micro, the program no longer exits when deleting the temp files. Re-enable Trend Micro, and sure enough, it gets killed. Now need to determine how to allow Trend Micro to run, but not kill my application!

twitort
  • 313
  • 2
  • 11