0

I need to show settings form for the user, save results to config file and always use the latest settings inside background thread of the main form.

What is the best way to avoid situations like race condition in this case (reading and writing to the config file simultaneously)? What is the best practice to do it in C# and Windows Forms?

To be more precise, I have global hotkey that opens settings form like this:

var settingsForm = new SettingsForm();
settingsForm.Show();

On this settings form I have two buttons -- "Ok" and "Cancel". When the user presses "Ok" button, new settings writing to config file.

In the main form I have a background thread with the infinite loop that do some stuff with the current settings.

Thanks in advance.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • "The best way" and "best practice" are opinion-based when you don't provide any specific requirements. There are various solutions, the feasibility of each of them depends on what your code actually does. This problem of course isn't unique to storing and retrieving application configuration. What have you found? Sharing your research helps. – CodeCaster Feb 21 '15 at 15:06
  • 1
    Using locks is a simple solution: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx – Aaron Feb 21 '15 at 15:06
  • @Aaron These are the different classes. Should I create another class with the static public lock then? – FrozenHeart Feb 21 '15 at 15:10
  • Reading and writing config file at the same time is NOT a race condition per se. It is better to clarify if you really have to prevent race condition, or you just need a safe way to save/read config in this situation. – tia Feb 21 '15 at 15:21
  • I'd use a private lock and you don't need to create a new class for that, but if you have a background loop like that it might me better to use the observer pattern and handle it in your background thread combined with async calls. – Aaron Feb 21 '15 at 15:22
  • @tia Sorry about it, I mean "safe way to save/read config" – FrozenHeart Feb 21 '15 at 15:23
  • @Aaron Can you give me an example, please? – FrozenHeart Feb 21 '15 at 15:23

1 Answers1

0

The settings in the Visual Studio project settings should be fine. The generated code will initialize an ApplicationBase that is synchonized (thread-safe) for you. You can easily save settings using Save() method and access the settings from Properties.Settings.Default from any thread.

tia
  • 9,518
  • 1
  • 30
  • 44
  • I have INI settings handling like this -- http://stackoverflow.com/questions/217902/reading-writing-an-ini-file – FrozenHeart Feb 21 '15 at 15:35
  • Move away from INI if you can, but if you already access your INI through WinAPI it should be no thread-safety issue. – tia Feb 21 '15 at 15:39
  • What do you mean by "Move away from INI if you can"? And wy do you think that "if you already access your INI through WinAPI it should be no thread-safety issue"? – FrozenHeart Feb 21 '15 at 15:41
  • I mean using another configuration file format instead of using INI format if you can. The INI format is very archaic. – tia Feb 21 '15 at 15:44
  • And what about the second question? – FrozenHeart Feb 21 '15 at 15:50