Reputation: 9585
I'm hooking into SystemEvents.UserPreferenceChanged event in a WPF application to detect when the user changes the regional format. I'm then attempting to determine the new region (CultureInfo) by clearing the culture cached data using ClearCachedData, starting a new thread and inspecting that thread's culture (original SO post here)
SystemEvents.UserPreferenceChanged += (sender, e) =>
{
// Regional settings have changed
if (e.Category == UserPreferenceCategory.Locale)
{
Thread.CurrentThread.CurrentCulture.ClearCachedData();
var thread = new Thread(s => ((State)s).Result =
Thread.CurrentThread.CurrentCulture);
var state = new State();
thread.Start(state);
thread.Join();
// This should be the new culture, but always returns the original culture
var culture = state.Result;
}
};
private class State
{
public CultureInfo Result { get; set; }
}
According to that post and the docs, calling ClearCachedData should mean any new threads start with the new culture, but I'm never seeing the new culture returned by the new thread, I always get the same culture as when the application started.
Using the win32 library method GetUserDefaultLCID seems to reliably return the expected culture, but it'd be nice to keep it all pure C# - am I doing something wrong in the above code?
Upvotes: 3
Views: 48