473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Design Question dealing with Multithreading

Hello,

Just some background: I'm developing an application that basically executes
series of tasks. So far I have 2 group of tasks run on different threads (2
different threads). Which run in parallel. Now, these threads need read-only
access to app config settings Object. So, I have one public class with 1
static field - Hashtable which holds key/value configs settings. So far,
application runs in debug mode without any errors.
Is that a good design? Do I need to lock or synchronize access to that
static member? What's the "best practice" design for this kind of scenario?

Thank you for your help.

Nov 17 '05 #1
8 1224
Just curious, if the config file is readonly, do you need a lock? Also,
I was told ny experts to avoid static methods as much as possible.

Nov 17 '05 #2
You should not need to lock the hash table if all you are doing is reading
from it. However, when you are creating it, that activity should take place
in a single thread.

Why are you loading your config settings into a hash table anyway? Why not
directly from XML into an object? That is usually easier to read and runs
much faster, since there is no need to look up the key each time you want a
config setting?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hello,

Just some background: I'm developing an application that basically
executes
series of tasks. So far I have 2 group of tasks run on different threads
(2
different threads). Which run in parallel. Now, these threads need
read-only
access to app config settings Object. So, I have one public class with 1
static field - Hashtable which holds key/value configs settings. So far,
application runs in debug mode without any errors.
Is that a good design? Do I need to lock or synchronize access to that
static member? What's the "best practice" design for this kind of
scenario?

Thank you for your help.

Nov 17 '05 #3
if the config file is readonly, do you need a lock
Without boring you with implementation details of this app. Default config
settings are stored in a file. But each user can and will change settings and
they get stored to IsolatedStorage, and loaded into a static field.
For the lifetime of an app config setting active threads do not access
config file or IsolatedStorage for this matter.

"DBC User" wrote:
Just curious, if the config file is readonly, do you need a lock? Also,
I was told ny experts to avoid static methods as much as possible.

Nov 17 '05 #4
Thank you all.
Why are you loading your config settings into a hash table anyway? Why not
directly from XML into an object?
Like my own type or structure? I thought about it. But config keys might
change or new onse will be added. So, I don't want to keep changing this
object all the time. I want to be able load settings dynamicly into an
object; hashtable does that for me.

"Nick Malik [Microsoft]" wrote:
You should not need to lock the hash table if all you are doing is reading
from it. However, when you are creating it, that activity should take place
in a single thread.

Why are you loading your config settings into a hash table anyway? Why not
directly from XML into an object? That is usually easier to read and runs
much faster, since there is no need to look up the key each time you want a
config setting?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hello,

Just some background: I'm developing an application that basically
executes
series of tasks. So far I have 2 group of tasks run on different threads
(2
different threads). Which run in parallel. Now, these threads need
read-only
access to app config settings Object. So, I have one public class with 1
static field - Hashtable which holds key/value configs settings. So far,
application runs in debug mode without any errors.
Is that a good design? Do I need to lock or synchronize access to that
static member? What's the "best practice" design for this kind of
scenario?

Thank you for your help.


Nov 17 '05 #5
I believe Hashtable by default supports one writer and multiple readers concurrently. Even if you need multiple writer support, that's available too using the thread-safe wrapper returned by the Synchronized() static method.
The FW supports a special attribute class for the synchronization needs of static members, ThreadStaticAttribute - it's behavior is quite different from that of a static variable, have a look though.

HTH, Metallikanz!

"Lenn" <Le**@discussions.microsoft.com> wrote in message news:E1**********************************@microsof t.com...
Hello,

Just some background: I'm developing an application that basically executes
series of tasks. So far I have 2 group of tasks run on different threads (2
different threads). Which run in parallel. Now, these threads need read-only
access to app config settings Object. So, I have one public class with 1
static field - Hashtable which holds key/value configs settings. So far,
application runs in debug mode without any errors.
Is that a good design? Do I need to lock or synchronize access to that
static member? What's the "best practice" design for this kind of scenario?

Thank you for your help.

Nov 17 '05 #6
I think the suggestion was to simply load the config into a config reader ...
for example:

/** ************************************** */
1. Create a static object for reading app.config file
/** ************************************** */

using System;
using System.Configuration;
using System.Xml;
using System.Windows.Forms;

sealed public class DynamicSettings
{
private static AppSettingsReader m_SettingsReader = null;
private static string m_ConfigFile = string.Empty;
private static XmlDocument m_XmlConfigurationFile = null;

static DynamicSettings()
{
m_SettingsReader = new AppSettingsReader();
m_ConfigFile = Application.ExecutablePath + ".config";
m_XmlConfigurationFile = new XmlDocument();
m_XmlConfigurationFile.Load(m_ConfigFile);
}

public static string GetString(string name)
{
return(((string)(m_SettingsReader.GetValue(name, typeof(string)))));
}
public static int GetInt(string name)
{
try
{
return(((int)(m_SettingsReader.GetValue(name, typeof(int)))));
}
catch { return(0); }
}
public static double GetDouble(string name)
{
try
{
return(((double)(m_SettingsReader.GetValue(name, typeof(double)))));
}
catch { return(0.0d); }
}
public static float GetFloat(string name)
{
try
{
return(((float)(m_SettingsReader.GetValue(name, typeof(float)))));
}
catch { return(0.0f); }
}
public static bool GetBoolean(string name)
{
try
{
return(((bool)(m_SettingsReader.GetValue(name, typeof(bool)))));
}
catch { return(false); }
}
public static void Set(string name, object value)
{
try
{
XmlNode node =
m_XmlConfigurationFile.SelectSingleNode("//configuration/appSettings/add[@key='" + name + "']");
node.Attributes["value"].Value = value.ToString();
m_XmlConfigurationFile.Save(m_ConfigFile);
}
catch { }
}
}

NOTE : I guess you have to be happy with static methods though :o))
Also, you will have to litter the accessors with

lock(typeof(DynamicSettings))
{
// ...
}

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Lenn" wrote:
Thank you all.
>Why are you loading your config settings into a hash table anyway? Why not
directly from XML into an object?


Like my own type or structure? I thought about it. But config keys might
change or new onse will be added. So, I don't want to keep changing this
object all the time. I want to be able load settings dynamicly into an
object; hashtable does that for me.

"Nick Malik [Microsoft]" wrote:
You should not need to lock the hash table if all you are doing is reading
from it. However, when you are creating it, that activity should take place
in a single thread.

Why are you loading your config settings into a hash table anyway? Why not
directly from XML into an object? That is usually easier to read and runs
much faster, since there is no need to look up the key each time you want a
config setting?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hello,

Just some background: I'm developing an application that basically
executes
series of tasks. So far I have 2 group of tasks run on different threads
(2
different threads). Which run in parallel. Now, these threads need
read-only
access to app config settings Object. So, I have one public class with 1
static field - Hashtable which holds key/value configs settings. So far,
application runs in debug mode without any errors.
Is that a good design? Do I need to lock or synchronize access to that
static member? What's the "best practice" design for this kind of
scenario?

Thank you for your help.


Nov 17 '05 #7
Interesting design, Bill, but that wasn't my suggestion. The OP got it
right... I was suggesting that he create an XML structure and load the
config settings into memory.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"billr" <bi***@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
I think the suggestion was to simply load the config into a config reader
...
for example:

/** ************************************** */
1. Create a static object for reading app.config file
/** ************************************** */

using System;
using System.Configuration;
using System.Xml;
using System.Windows.Forms;

sealed public class DynamicSettings
{
private static AppSettingsReader m_SettingsReader = null;
private static string m_ConfigFile = string.Empty;
private static XmlDocument m_XmlConfigurationFile = null;

static DynamicSettings()
{
m_SettingsReader = new AppSettingsReader();
m_ConfigFile = Application.ExecutablePath + ".config";
m_XmlConfigurationFile = new XmlDocument();
m_XmlConfigurationFile.Load(m_ConfigFile);
}

public static string GetString(string name)
{
return(((string)(m_SettingsReader.GetValue(name,
typeof(string)))));
}
public static int GetInt(string name)
{
try
{
return(((int)(m_SettingsReader.GetValue(name, typeof(int)))));
}
catch { return(0); }
}
public static double GetDouble(string name)
{
try
{
return(((double)(m_SettingsReader.GetValue(name,
typeof(double)))));
}
catch { return(0.0d); }
}
public static float GetFloat(string name)
{
try
{
return(((float)(m_SettingsReader.GetValue(name,
typeof(float)))));
}
catch { return(0.0f); }
}
public static bool GetBoolean(string name)
{
try
{
return(((bool)(m_SettingsReader.GetValue(name, typeof(bool)))));
}
catch { return(false); }
}
public static void Set(string name, object value)
{
try
{
XmlNode node =
m_XmlConfigurationFile.SelectSingleNode("//configuration/appSettings/add[@key='"
+ name + "']");
node.Attributes["value"].Value = value.ToString();
m_XmlConfigurationFile.Save(m_ConfigFile);
}
catch { }
}
}

NOTE : I guess you have to be happy with static methods though :o))
Also, you will have to litter the accessors with

lock(typeof(DynamicSettings))
{
// ...
}

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
"Lenn" wrote:
Thank you all.
>Why are you loading your config settings into a hash table anyway? Why

not
> directly from XML into an object?


Like my own type or structure? I thought about it. But config keys might
change or new onse will be added. So, I don't want to keep changing this
object all the time. I want to be able load settings dynamicly into an
object; hashtable does that for me.

"Nick Malik [Microsoft]" wrote:
> You should not need to lock the hash table if all you are doing is
> reading
> from it. However, when you are creating it, that activity should take
> place
> in a single thread.
>
> Why are you loading your config settings into a hash table anyway? Why
> not
> directly from XML into an object? That is usually easier to read and
> runs
> much faster, since there is no need to look up the key each time you
> want a
> config setting?
>
> --
> --- Nick Malik [Microsoft]
> MCSD, CFPS, Certified Scrummaster
> http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
> I do not answer questions on behalf of my employer. I'm just a
> programmer helping programmers.
> --
> "Lenn" <Le**@discussions.microsoft.com> wrote in message
> news:E1**********************************@microsof t.com...
> > Hello,
> >
> > Just some background: I'm developing an application that basically
> > executes
> > series of tasks. So far I have 2 group of tasks run on different
> > threads
> > (2
> > different threads). Which run in parallel. Now, these threads need
> > read-only
> > access to app config settings Object. So, I have one public class
> > with 1
> > static field - Hashtable which holds key/value configs settings. So
> > far,
> > application runs in debug mode without any errors.
> > Is that a good design? Do I need to lock or synchronize access to
> > that
> > static member? What's the "best practice" design for this kind of
> > scenario?
> >
> > Thank you for your help.
> >
>
>
>

Nov 17 '05 #8
"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
Thank you all.
Why are you loading your config settings into a hash table anyway? Why
not
directly from XML into an object?


Like my own type or structure? I thought about it. But config keys might
change or new onse will be added. So, I don't want to keep changing this
object all the time. I want to be able load settings dynamicly into an
object; hashtable does that for me.


Hi Lenn,

I agree with your approach. Don't optimize for speed until you are code
complete. However, if you are running your speed tests, and find you need
to improve, this approach may be useful.

Good luck,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
5
by: istillshine | last post by:
Particularly for medium-sized (10,000 ~ 20,000 lines) programs, what are useful strategies to design them before coding? My strategies are: 1. Imagine what the final program would look like....
1
by: Angus | last post by:
Hi all I have a design which models a telephone system. Roughly the design has these two items: device class which models an extension on the telephone system. The device constructor takes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.