[英]Correct way to use ResourceManager in a multi-threaded environment in C#?
So, I currently hold an instance of the ResourceManager in a static variable accessible for all classes in my solution. 因此,我目前将ResourceManager的实例保存在解决方案中所有类均可访问的静态变量中。 I've used it for getting translation stuff for single-threaded operation and it was working just fine and dandy.
我已经使用它来获取单线程操作的翻译内容,并且效果很好。
However, I then added an operation that takes a bit longer to execute, and I need to update the progress in the UI. 但是,然后我添加了一个需要花费更长的时间才能执行的操作,并且需要更新UI中的进度。 For the progress information strings, I also use the ResourceManager.
对于进度信息字符串,我还使用ResourceManager。 Thing is, the same exact line of code returns null (Localization.ResourceManager.GetString("ProgressInformation1"), for example) if it is executed on a separate thread.
事实是,如果在单独的线程上执行,则相同的确切代码行将返回null(例如,Localization.ResourceManager.GetString(“ ProgressInformation1”))。 But if I don't fire a new thread for it, it returns the string as expected.
但是,如果我不为其触发新线程,它将按预期返回字符串。 I checked the MSDN library, and according to it, ResourceManager should be thread-safe and all.
我检查了MSDN库,根据它,ResourceManager应该是线程安全的。 So what am I doing wrong?
那我在做什么错?
Perhaps you need to specify CurrentCulture and CurrentUICulture for the thread you run. 也许您需要为运行的线程指定CurrentCulture和CurrentUICulture。 Just use ones from the "main" thread.
只需使用“主”线程中的线程即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.