这段代码线程安全吗? C#

时间:2012-04-07 21:02:33

标签: c# multithreading

许多代码已被删除,但我真的只需要显示这一部分。主要思想:方法 Localize()必须快速工作,因此不能被任何其他线程锁定。它使用标志 isLoaded 来检测要使用的本地化类型 - 旧的或当前的。

static class Localization
{
    static readonly object locker = new object();

    static string LocalizationDir;

    static bool isLoaded;
    static Tuple<string, string> OldLanguage;
    static Dictionary<string, string> OldLocalization;
    static Dictionary<string, string> CurrentLocalization;

    public static Tuple<string, string> CurrentLanguage {
        get;
        private set;
    }

    static Localization() {
        lock ( locker ) {
            OldLanguage = null;
            CurrentLanguage = new Tuple<string, string>("en", "English");
            isLoaded = true;

            OldLocalization = null;
            CurrentLocalization = null;
        }
    }

    public static bool SetLanguage(string languageShortName) {
        lock ( locker ) {
            string languagePath = Path.Combine(LocalizationDir, languageShortName + ".loc");

            // save localization, be ready to return it back
            OldLocalization = CurrentLocalization;
            OldLanguage = CurrentLanguage;
            isLoaded = false;

            try {
                using ( TextReader i = new StreamReader(languagePath) ) {
                    /*
                        Parse file, 
                        Modify CurrentLocalization, CurrentLocalization
                    */
                }
            }
            catch ( Exception e ) {
                // Just return back our good localization data

                CurrentLocalization = OldLocalization;
                CurrentLanguage = OldLanguage;
                isLoaded = true;

                OldLocalization = null;
                OldLanguage = null;

                return false;
            }

            // everything is good
            {
                OldLocalization = null;
                OldLanguage = null;
                isLoaded = true;

                UpdateControls();
            }

            return true;
        }
    }


    // <summary>
    // We think that there are no bugs in this method
    // No locking
    // </summary>
    public static string Localize(this string Text) {
        if ( CurrentLanguage.Item2 == "English" )
            return Text;

        Dictionary<string, string> ChoosedLocalization = null;
        if ( !isLoaded && OldLocalization != null )
            ChoosedLocalization = OldLocalization;
        else if ( isLoaded && CurrentLocalization != null )
            ChoosedLocalization = CurrentLocalization;

        if ( ChoosedLocalization != null ) {
            string Translate;

            if ( !ChoosedLocalization.TryGetValue(Text, out Translate) )
                return Text;
            else
                return Translate;
        }
        else
            return Text;
    }
} 

1 个答案:

答案 0 :(得分:2)

我怀疑不是。我也怀疑你是在过度思考。在SetLanguage中,你应该做的就是加载新的字典,然后将它换成旧字典。那个赋值是原子的,所以只要你的Translate代码不连续两次使用CurrentLanguage并假设它将是相同的并且UpdateControls调用等待直到前一个调用完成(或其他一些竞争条件预防)然后它应该更简单,更清洁,更可靠。