Reputation: 7805
So I have a WPF application written in .NET 4.0. In the application, there is an import button, which does an OpenFileDialog searching for an XML file. After it opens the file, it reads through the XML, and retrieves the data from it and stores it in Properties.Settings.Default. Basically, its a system where people can export and import settings. Works nice.
Now, what I want to do is that after an import, it saves the new settings, then KILLS the application and restarts it. Simple, saves settings, starts new instance of the app, kills the old instance.
Properties.Settings.Default.Save();
System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
System.Diagnostics.Process.GetCurrentProcess().Kill();
However, the problem is that often the start of the new instance of the app is before the settings finish saving. So the new instance starts up retrieving the old settings. I figured I could solve this by adding a simple sleep to the process; but this is very hacky.
System.Threading.Thread.Sleep(1000);
What if the save process takes more than 1000 milliseconds? Then the old issue still occurs. I know I can just increase the sleep timer, but I'm sure there has to be a better way. Is there a way to have it so that the last 2 lines dont activate until after the first line finishes?
Upvotes: 0
Views: 127
Reputation: 184296
Never heard of Save
being asynchronous, as last resort you could use a FileSystemWatcher
to monitor the affected file and call your code when there are no more changes to it.
Upvotes: 1
Reputation: 15232
Why not have your save method perform a return when it done saving or have it call the Process Start. in that case you shouldn't be able to start the app until the first is finished saving
Upvotes: 0