Click here to Skip to main content
16,011,436 members
Home / Discussions / C#
   

C#

 
GeneralRe: excel/C# Pin
MarkB77711-Apr-08 13:15
MarkB77711-Apr-08 13:15 
GeneralTable Adapter Object Pin
ctrlnick11-Apr-08 7:17
ctrlnick11-Apr-08 7:17 
QuestionHow to display empty folder in after deployed. Pin
bruze11-Apr-08 6:52
bruze11-Apr-08 6:52 
General[Message Deleted] Pin
bruze11-Apr-08 5:53
bruze11-Apr-08 5:53 
GeneralRe: How to Disply the Empty folder in After Deployed Pin
Abhijit Jana11-Apr-08 6:09
professionalAbhijit Jana11-Apr-08 6:09 
GeneralInteracting with Messenger Pin
rcollina11-Apr-08 4:34
rcollina11-Apr-08 4:34 
GeneralRe: Interacting with Messenger Pin
Luis Alonso Ramos11-Apr-08 11:46
Luis Alonso Ramos11-Apr-08 11:46 
GeneralThread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once [modified] Pin
Waleed Eissa11-Apr-08 3:25
Waleed Eissa11-Apr-08 3:25 
I'm trying to write an ASP.NET application with thread safety in mind (not one of my strength points). I use the static class shown below for the application settings (not the real code, just a simplified version):
public static class AppSettings
{
   private static int _setting1;
   public static int Setting1
   {
      get { return _setting1; }
   }

   private static string _setting2;
   public static string Setting2
   {
      get { return _setting2; }
   }

   static AppSettings()
   {
      LoadSettings();
   }

   public static LoadSettings()
   {
      // Call the DAL here and load the settings from the database.
      // The values returned from the DAL are assigned to 
      // _setting1 and _setting2
   }

   // This method's only purpose is to make it possible to
   // reload the settings dynamically while the application
   // is already running
   public static ReloadSettings()
   {
      LoadSettings();
   }
}

As you can see in the code above, LoadSettings() is called from two locations, the first is from the static constructor (no problem here, the static constructor is thread safe) and from ReloadSettings() (where the problem is) what I'm trying to do is that I want to block all threads from accessing the settings in the private members (accessed through the properties) until all the settings are updated, is this possible using a static class?

I figured out that I could change the code to be as below and the problem will be solved:
public static class AppSettings
{
   private static AppSettingsData _appSettingsData;

   public static int Setting1
   {
      get { return _appSettingsData.Setting1; }
   }

   public static string Setting2
   {
      get { return _appSettingsData.Setting2; }
   }

   static AppSettings()
   {
      LoadSettings();
   }

   public static LoadSettings()
   {
      int setting1;
      string setting2;

      // Call the DAL here and load the settings from the database
      // the values returned from the DAL are assigned to 
      // setting1 and setting2

      AppSettingsData settingsData = new AppSettingsData(setting1, setting2);
      _appSettingsData = settingsData;
   }

   // This method's only purpose is to make it possible
   // to reload the settings dynamically while the application
   // is already running
   public static ReloadSettings()
   {
      LoadSettings();
   }
}

public class AppSettingsData
{
   public AppSettingsData(int setting1, string setting2)
   {
      _setting1 = setting1;
      _setting2 = setting2;
   }

   private int _setting1;
   public int Setting1
   {
      get { return _setting1; }
   }

   private string _setting2;
   private string Setting2
   {
      get { return _setting2; }
   }   
}

Although this would solve the problem, I actually still prefer to use a static class for everything (unless of course it's not possible using a static class), and also out of curiosity I would like to know whether this is possible or not with only a static class.

Your help is very much appreciated...


modified on Friday, April 11, 2008 9:31 AM

GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
PIEBALDconsult11-Apr-08 3:40
mvePIEBALDconsult11-Apr-08 3:40 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
N a v a n e e t h11-Apr-08 3:42
N a v a n e e t h11-Apr-08 3:42 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Judah Gabriel Himango11-Apr-08 4:28
sponsorJudah Gabriel Himango11-Apr-08 4:28 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
N a v a n e e t h11-Apr-08 7:06
N a v a n e e t h11-Apr-08 7:06 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Judah Gabriel Himango11-Apr-08 7:22
sponsorJudah Gabriel Himango11-Apr-08 7:22 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Waleed Eissa11-Apr-08 5:10
Waleed Eissa11-Apr-08 5:10 
GeneralRe: Thread Safety Question: How to 'Safely' Change the Values of Multiple Static Variables at Once Pin
Waleed Eissa11-Apr-08 5:00
Waleed Eissa11-Apr-08 5:00 
GeneralCustom C# .NET user control Pin
cloud strife11-Apr-08 3:21
cloud strife11-Apr-08 3:21 
QuestionProblem when passing arrays from c# to matlab Pin
sarjo11-Apr-08 3:08
sarjo11-Apr-08 3:08 
GeneralHelp me Pin
prachi1411-Apr-08 3:00
prachi1411-Apr-08 3:00 
GeneralRe: Help me Pin
carbon_golem11-Apr-08 3:04
carbon_golem11-Apr-08 3:04 
GeneralRe: Help me Pin
Pete O'Hanlon11-Apr-08 3:04
mvePete O'Hanlon11-Apr-08 3:04 
GeneralRe: Help me Pin
prachi1422-Apr-08 0:39
prachi1422-Apr-08 0:39 
GeneralRe: Help me Pin
Roger Alsing11-Apr-08 3:08
Roger Alsing11-Apr-08 3:08 
GeneralRe: Help me Pin
CPallini11-Apr-08 4:09
mveCPallini11-Apr-08 4:09 
GeneralRe: Help me Pin
Ashfield11-Apr-08 4:44
Ashfield11-Apr-08 4:44 
GeneralRe: Help me Pin
Abhijit Jana11-Apr-08 6:12
professionalAbhijit Jana11-Apr-08 6:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.