by Michael S. Kaplan, published on 2005/04/05 02:02 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2005/04/05/405443.aspx
A few days ago I talked about how ClearCachedData found its way into the .NET Framework. I told the whole fun story, and I waited hopefully for someone to ask the question.
I explained how it was too late to do anything more extensive in the first version of the .NET Framework. And held my breath waiting for the question.
Alas, the question did not come.
What was the question, you may ask?
The question was "so what happened in version 1.1?" I mean, we cannot refuse to address issues in a later release that are "too late to address" in the current relese, can we?
I assume everyone was just so beguiled by the excellent storytelling job I did that no one noticed.
Well, technically one person asked the question, but I believe he is the one who was behind it so I couldn't count him. :-)
Version 1.1 of the .NET Framework (Everett) added the SystemEvents.UserPreferenceChanged event!
By using the UserPrefereceChangedEventArgs.Category of UserPreferenceCategory.Locale, you will be getting the same notification that the WM_SETTINGCHANGE message with a wParam of 0 and an lParam of "intl". The very message that means someone has changed a setting in Regional Options!
Unfortunately it cannot tell you what has changed, but then again the WM_SETTINGCHANGE cannot tell you either. But it can tell you that something has changed. So if you are caching anything, you have to dump your cache. But that is just what CultureInfo.ClearCachedData is meant to do, so everything works out quite nicely. Like a marriage made in Arkon, as my 7th grade teacher would have said....
Now there is no specific sample for using this event for locales, but I figured no body would mind if I whipped one up here for this post.
First create the event procedure in your main class:
public void UserPreferenceChanged(object sender,
Microsoft.Win32.UserPreferenceChangedEventArgs e) {
switch(e.Category)
{
case UserPreferenceCategory.Locale:
Thread.CurrentThread.CurrentCulture.ClearCachedData();
break;
default:
break;
}
}
Then to make sure it gets called by adding the following in your constructor:
Microsoft.Win32.SystemEvents.UserPreferenceChanged
+= new Microsoft.Win32.UserPreferenceChangedEventHandler(
this.UserPreferenceChanged);
Finally, make sure to add the following to the form's Dispose event to unhook the event:
Microsoft.Win32.SystemEvents.UserPreferenceChanged
-= new Microsoft.Win32.UserPreferenceChangedEventHandler(
this.UserPreferenceChanged);
And that is all you have to do; let the .NET Framework do the rest....
One final thought -- don't look longingly at the SystemEvents.UserPreferenceChanging event, it does not really add anything here. :-)
This post brought to you by "Ļ" (U+013b, a.k.a. LATIN CAPITAL LETTER L WITH CEDILLA)
A letter that for some unknown reason was quite excited about the notion of hooking the "Locale changing" event!
# Eric on 5 Apr 2005 9:18 AM:
# Michael S. Kaplan on 5 Apr 2005 10:29 AM:
# Eric on 5 Apr 2005 11:43 AM:
# Michael S. Kaplan on 5 Apr 2005 12:29 PM:
# Ruben on 5 Apr 2005 4:11 PM:
# Michael S. Kaplan on 6 Apr 2005 9:18 AM:
# Scott on 20 Apr 2005 10:54 AM:
referenced by