<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: What&#8217;s wrong with this code? #1 &#8211; Discussion</title>
	<atom:link href="http://www.developerzen.com/2007/07/15/whats-wrong-with-this-code-1-discussion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developerzen.com/2007/07/15/whats-wrong-with-this-code-1-discussion/</link>
	<description>The essence of software development...</description>
	<lastBuildDate>Mon, 08 Mar 2010 17:49:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ekampf</title>
		<link>http://www.developerzen.com/2007/07/15/whats-wrong-with-this-code-1-discussion/comment-page-1/#comment-1754</link>
		<dc:creator>ekampf</dc:creator>
		<pubDate>Sat, 28 Mar 2009 15:19:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.ekampf.com/blog/PermaLink,guid,a609c210-498f-47b8-a0cc-984a43100a45.aspx#comment-1754</guid>
		<description>Hi Chan,
  As specified in MSDN C# spec (http://msdn.microsoft.com/en-us/library/aa645612.aspx) static constructors run at most once per app domain. Its even more specific in &quot;Managed Threading Best Practices&quot; (http://msdn.microsoft.com/en-us/library/1c9txz50(VS.80).aspx):

&lt;blockquote&gt;&quot;A class is not initialized until its class constructor (static constructor in C#, Shared Sub New in Visual Basic) has finished running. To prevent the execution of code on a type that is not initialized, the common language runtime blocks all calls from other threads to static members of the class (Shared members in Visual Basic) until the class constructor has finished running.

For example, if a class constructor starts a new thread, and the thread procedure calls a static member of the class, the new thread blocks until the class constructor completes.

This applies to any type that can have a static constructor.&quot;&lt;/blockquote&gt;

So, static constructors are definitely thread safe and you&#039;ll probably hitting some other error...  What kind of exception are you getting?</description>
		<content:encoded><![CDATA[<p>Hi Chan,<br />
  As specified in MSDN C# spec (<a href="http://msdn.microsoft.com/en-us/library/aa645612.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa645612.aspx</a>) static constructors run at most once per app domain. Its even more specific in &#8220;Managed Threading Best Practices&#8221; (<a href="http://msdn.microsoft.com/en-us/library/1c9txz50(VS.80" rel="nofollow">http://msdn.microsoft.com/en-us/library/1c9txz50(VS.80</a>).aspx):</p>
<blockquote><p>&#8220;A class is not initialized until its class constructor (static constructor in C#, Shared Sub New in Visual Basic) has finished running. To prevent the execution of code on a type that is not initialized, the common language runtime blocks all calls from other threads to static members of the class (Shared members in Visual Basic) until the class constructor has finished running.</p>
<p>For example, if a class constructor starts a new thread, and the thread procedure calls a static member of the class, the new thread blocks until the class constructor completes.</p>
<p>This applies to any type that can have a static constructor.&#8221;</p></blockquote>
<p>So, static constructors are definitely thread safe and you&#8217;ll probably hitting some other error&#8230;  What kind of exception are you getting?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chan</title>
		<link>http://www.developerzen.com/2007/07/15/whats-wrong-with-this-code-1-discussion/comment-page-1/#comment-1753</link>
		<dc:creator>Chan</dc:creator>
		<pubDate>Fri, 27 Mar 2009 23:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.ekampf.com/blog/PermaLink,guid,a609c210-498f-47b8-a0cc-984a43100a45.aspx#comment-1753</guid>
		<description>Hi,
I am using static constructor as below, it hit error if Eval() is called by more than one person at the same time. It works time if I added lock(). However, as you mentioned static constructor should be thread safe. Shall you please explain about this ?

Thank you

&lt;pre&gt;
public class Evaluator
    {
        private static object _evaluator = null;
        private static Type _evaluatorType = null;
        private static readonly string _jscriptSource = @&quot;
                                                        package Evaluator
                                                        {
                                                            class Evaluator
                                                            {
                                                                public function Eval(expr : String) : String
                                                                {
                                                                    return eval(expr,&quot;&quot;unsafe&quot;&quot;);
                                                                }
                                                            }
                                                        }&quot;;

        static Evaluator()
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider(&quot;JScript&quot;);

            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;

            CompilerResults results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

            Assembly assembly = results.CompiledAssembly;
            _evaluatorType = assembly.GetType(&quot;Evaluator.Evaluator&quot;);

            _evaluator = Activator.CreateInstance(_evaluatorType);
        }

        public static object EvalToObject(string expression)
        {
            object result;

            try
            {
                result = _evaluatorType.InvokeMember(
                    &quot;Eval&quot;,
                    BindingFlags.InvokeMethod,
                    null,
                    _evaluator,
                    new object[] { expression }
                    );
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(&quot;Evaluator: Failed to evaluate expression {0} - {1}&quot;, expression, ex.Message));
            }

            return result;
        }

        public static string Eval(string expression)
        {
            object o = EvalToObject(expression);
            return o.ToString();
        }
    }
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I am using static constructor as below, it hit error if Eval() is called by more than one person at the same time. It works time if I added lock(). However, as you mentioned static constructor should be thread safe. Shall you please explain about this ?</p>
<p>Thank you</p>
<pre>
public class Evaluator
    {
        private static object _evaluator = null;
        private static Type _evaluatorType = null;
        private static readonly string _jscriptSource = @"
                                                        package Evaluator
                                                        {
                                                            class Evaluator
                                                            {
                                                                public function Eval(expr : String) : String
                                                                {
                                                                    return eval(expr,""unsafe"");
                                                                }
                                                            }
                                                        }";

        static Evaluator()
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("JScript");

            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;

            CompilerResults results = provider.CompileAssemblyFromSource(parameters, _jscriptSource);

            Assembly assembly = results.CompiledAssembly;
            _evaluatorType = assembly.GetType("Evaluator.Evaluator");

            _evaluator = Activator.CreateInstance(_evaluatorType);
        }

        public static object EvalToObject(string expression)
        {
            object result;

            try
            {
                result = _evaluatorType.InvokeMember(
                    "Eval",
                    BindingFlags.InvokeMethod,
                    null,
                    _evaluator,
                    new object[] { expression }
                    );
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Evaluator: Failed to evaluate expression {0} - {1}", expression, ex.Message));
            }

            return result;
        }

        public static string Eval(string expression)
        {
            object o = EvalToObject(expression);
            return o.ToString();
        }
    }
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
