I made an auto update of the service by reading the version from the xml document on the site. Put on the timer to read every minute (for the test). The service hangs after the second tick. I guess that it cannot load the XmlDocument.Load (String) a second time, because it is already open. How to close it?

  • The debugger is silent - Roman Sibiryak
  • Where is the code? Does the execution exactly fall into this method a second time? set a breakpoint, check. What timer do you use? - Andrey NOP
  • and if in Load to transfer not String but for example Filestream which will accept String, FileStream to wrap in using, Dispose () will be called, or the GC.Collect () variant. - Ruslan_K
  • Thanks for the answers, it's not about xmldocument.load .. Just noticed this bug after adding it .. - Roman Sibiryak
  • To make the debugger speak, try torturing it. Push in breakpoints around the code that loads the document, go through this piece step by step. You can hang it on the banal waiting for a response from the server. - rdorn

2 answers 2

XmlDocument.Load closes the file immediately after it is loaded. The following code does not lead to errors.

XmlDocument t = new XmlDocument(); t.Load(@"test.xml"); t.Load(@"test.xml"); t.Load(@"test.xml"); 

Guaranteed to kill an object like this:

 t = null; GC.Collect(); 

But the problem is clearly in something else.

  • Thanks for the answer. Indeed, you are right, now I tried to debug it for a long time, I don’t give out any errors. I removed all the functionality in the service, except for the start on timer, the timer stops working after 45-50 seconds. In service, this is such a garbage - Roman Sibiryak
  • I use the timer from System.Threading - Roman Sibiryak
  • That is, the timer correctly launches the application 3 times (3 ticks at the moment), of course, if it is not already running. For 4 times, nothing starts anymore .. The same code in the console application works stably. there is no difference in the code between the console application and the service (removed all the functionality temporarily) - Roman Sibiryak
  • public void Timer () {int num = 0; TimerCallback tm = new TimerCallback (Start); Timer Timer = new Timer (tm, num, 0, 15000); } - Roman Sibiryak

Thank you all, the problem was really different, for some reason the timer from Threading broke down after a minute, replaced it with the timer from Timers and everything worked as it should.