Modelon Solutions

A fresh take on your ideas

Async, threading and cultures

Published by

on

Well, I bit a small bug in a one of the systems we deployed a few weeks ago that imports massive amounts of sales and inventory data. The program itself can run on any computer within the network, and the problem showed up when we ran it off a Windows 7 computer installed in French.

It didn’t take a lot of time to realize that the default culture on the computer was off, which was creating a problem when parsing the numbers in the csv file. But the question how to fix it…

At any time, you can do something like Thread.CurrentThread.CurrentCulture (or CurrentUICulture) and that will change the current thread’s culture. That is good, but what do you do when you don’t really have a lot of async code and a parallelism ?

The answer lies in a new feature in .net 4.5. You can basically change the current culture for all threads in a AppDomain with a single call. For threads that already exist, if there culture hasn’t yet been overridden using Thread.CurrentThread.CurrentCulture (or CurrentUICulture), it will pickup the new culture information specified.

CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture are the two new properties you have to learn; they are described more in depth on msdn : http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.defaultthreadcurrentculture(v=vs.110).aspx

Enjoy !

Leave a comment