-1

I have a routine that reads XPS documents and chops off pages to separate documents. Originally it read one document, decided how to chop it, closed it and wrote out the new files.

Features were added, this was causing headaches with cleaning up old files before running the routine and so I saved all the chopped pieces to be written out at the end.

ChoppedXPS is a dictionary, the key is the filename, the data is the FixedDocument prepared from the original:

foreach (String OneReport in ChoppedXPS.Keys)
{
    File.Delete(OneReport);
    using (XpsDocument TargetFile = new XpsDocument(OneReport, FileAccess.ReadWrite))
    {
        XpsDocumentWriter Writer = XpsDocument.CreateXpsDocumentWriter(TargetFile);
        Writer.Write(ChoppedXPS[OneReport]);
        Logger($"{OneReport} written to disk", 2);
    }
    Application.DoEvents();
}

If the FixedDocument being written out here contains graphics the source file is opened by the Writer.Write line and left open until the program is closed.

The XpsDocumentWriter does not seem to implement anything that can be used to clean it up.

(Yeah, that Application.DoEvents is ugly--this is an in-house program used by two people, it's not worth the hassle of making this run in the background and without it a big enough task can cause Windows to decide it's non-responsive and kill it. And, yes, I know how to indent--I took them out to make it all fit this screen.)

.Net 4.5, using some C# 8.0 features.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
  • 2
    Please provide a [mcve]. – mjwills Nov 16 '20 at 03:45
  • Its been a loooong time since i have used `DoEvents` or should i say, learnt not to use `DoEvents`. However there is a small chance this is a re-entrance issue. Get rid of it. if you can, run this code in a task, await that task, protect the event from being called twice. Your UI will be active, you will have removed the need for `DoEvents` and you will have made sure there is no re-entrance problem. Otherwise it would seem there is bug with XpsDocument, which i find a lot more unlikely. – TheGeneral Nov 16 '20 at 04:39
  • @TheGeneral DoEvents isn't the culprit--the errant file open is occurring on Writer.Write. – Loren Pechtel Nov 16 '20 at 18:03
  • @LorenPechtel so you have canceled out DoEvents ? i mean, when you remove it, it still happens? have you assure your self that this method doesn't get called twice ? – TheGeneral Nov 16 '20 at 22:46
  • @TheGeneral Yes, it still happens. I have found a workaround--wrapping the using in a using of the source file. – Loren Pechtel Nov 17 '20 at 01:09
  • @LorenPechtel I am interested in your work around, post an answer, it might be helpful for others – TheGeneral Nov 17 '20 at 01:09

1 Answers1

0

I found a workaround for this problem. I'm not going to try to post the whole thing as I had to change the whole data handling but the heart of it:

using (XPSDocument Source = new XPSDocument(SourceFile, FileAccess.Read)
{
    [the using loop from my question]
}

I'm still hoping for understanding and something more appropriate than this approach.

Yes--this produces a warning that Source is unused, but the compiler isn't eliminating it so it does work.

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45