繁体   English   中英

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

[英]Is this code thread safe? C#

删除了很多代码,但我真的只需要展示这一部分。 主要思想:方法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;
    }
} 

我怀疑不是。 我也怀疑你想多了。 在 SetLanguage 中,您应该做的就是加载新词典,然后将其换成旧词典。 该分配是原子的,因此只要您的翻译代码不连续两次使用 CurrentLanguage 并假设它是相同的并且 UpdateControls 调用等待直到上一个调用完成(或其他一些竞争条件预防),那么它应该更简单、更清洁、更可靠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM