I have a .NET custom control that displays information in a culture dependant way. To improve performance I cache some information inside the control rather than generate it every time. I need to know if the culture has changed so that I can regenerate this internal info as appropriate.
Is there some event I have hook into? Or do I have to just test the culture setting every time I paint to see if it has changed?
You can either handle the SystemEvents.UserPreferenceChanged
event:
SystemEvents.UserPreferenceChanged += (sender, e) =>
{
// Regional settings have changed
if (e.Category == UserPreferenceCategory.Locale)
{
// .NET also caches culture settings, so clear them
CultureInfo.CurrentCulture.ClearCachedData();
// do some other stuff
}
};
Windows also broadcasts the WM_SETTINGSCHANGE
message to all Forms. You could try detecting it by overriding WndProc, so it would look something like:
// inside a winforms Form
protected override void WndProc(ref Message m)
{
const int WM_SETTINGCHANGE = 0x001A;
if (m.Msg == WM_SETTINGCHANGE)
{
// .NET also caches culture settings, so clear them
CultureInfo.CurrentCulture.ClearCachedData();
// do some other stuff
}
base.WndProc(ref m);
}
Note that you probably also want to call CultureInfo.CurrentCulture.ClearCachedData()
to invalidate the culture info for newly created threads, but keep in mind that CurrentCulture
for existing threads won't be updated, as mentioned on MSDN:
The
ClearCachedData
method does not refresh the information in theThread.CurrentCulture
property for existing threads. However, future threads will have newCultureInfo
property values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With