0

Possible Duplicate:
Is XMLDocument.Save an atomic operation?

I appear to have a Heisenbug in my program. I need to export an Xml file and then re-import it. The problem is that sometimes before the Xml file has finished saving the program will continue to the next line to try and re-import it and because it is not finished saving the application will crash. When I debug it there is enough time for the file to save so the program doesn't crash.

I could use Thread.Sleep but on slower computers it might still crash if it took too long.

I'm using XmlDocument.Save to save the file. I was wondering if there might be a way to implement a callback of some sort for when the file has been completely saved.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • 4
    `Save` shouldn't return before the file has been saved. It may still be flushing through the OS caches, but anything accessing it should still work. Can you produce a short but complete program demonstrating the problem? – Jon Skeet Nov 05 '12 at 17:41
  • 2
    Here is a previous Question which is similar on SO. [http://stackoverflow.com/questions/2678079/is-xmldocument-save-an-atomic-operation][1] [1]: http://stackoverflow.com/questions/2678079/is-xmldocument-save-an-atomic-operation – Totero Nov 05 '12 at 17:44

1 Answers1

4

Suspect antivirus. Many virus scanners aren't written well and lock the file when you close it, preventing any program from opening it for a few seconds.

See this question:

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    I think you are correct with the anti-virus because the anti-virus on the computer is quite invasive. Would the best solution to this just be a try-catch with a timeout that tries x amount of times? – Ryan Peschel Nov 05 '12 at 18:10
  • @Ryan: The linked question and answers come to that conclusion. It is probably the best solution you have just by changing your code, without configuration changes to the whole computer. Even better would be to get an AV program that isn't broken. – Ben Voigt Nov 05 '12 at 19:12