title: App.Current.Shutdown not letting DataSet.WriteXml complete results in corrupt config file
body: <p>Environment - C#, .NET 4.0, WPF, VS2010</p>
  
  <p>I have an app that uses a keyboard hook to detect when a combination of four keys is pressed. When this occurs it calls Configuration.Save(), which has a call to myConfigDataSet.WriteXml(myConfigFile). And then on the next line it calls App.Current.Shutdown().</p>
  
  <p>About half the time it works as expected. But many times it would insert XML content right into the middle of a previously existing configuration file, resulting in corrupt data.</p>
  
  <p>I was able to fix the above issue by using...</p>
  
  <p>if(File.Exists(myConfigFile)) { File.Delete(myConfigFile) }</p>
  
  <p>...on the line just above the call to myConfigDataSet.WriteXml(myConfigFile)</p>
  
  <p>But now many times it just writes a 0KB size file. I am pretty sure that all of my problems are being caused by App.Current.Shutdown() not waiting for the call to myConfigDataSet.WriteXml(myConfigFile) to finish.</p>
  
  <p>Shouldn't this call block execution until the file has been written to disk? ...apparently not!!!</p>
  
  <p>As a workaround I've already tried inserting Thread.Sleep(1000) to create a 1 second delay just before App.Current.Shutdown. But now sometimes the app errors out with a "StackOverFlow" exception...!!!</p>
  
  <p>What would you guys recommend as a fix or workaround for this? What am I doing wrong?</p>