Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

c# - How can I change the CurrentCulture of the entire process (not just current thread) in .Net?

I have a situation where I need to set my process' locale to en-US.

I know how to do this for the current thread:

System.Threading.Thread.CurrentThread.CurrentCulture = 
     System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

But my application uses BackgroundWorkers to do some processing, and the locale for these worker threads seems not to be affected by the above change to their spawning main-thread.

So how can I set the locale for all the threads in my application without setting it in each one manually?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll have to change the operating system locale if you want to do that. For what reason do you want BackgroundWorkers to run in en-US?

You should have your business layer running in an invariant culture, and only have a specific culture for the end user's UI.

If you are using the BackgroundWorker component, and have to do this you could try something like this in the DoWork method:

// In DoWork
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try

{
    System.Threading.Thread.CurrentThread.CurrentCulture = 
        new System.Globalization.CultureInfo("en-US");
 // Proceed with specific code
}

finally
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...