将CurrentCulture设置为Task creator CurrentCulture来执行任务

我有一个使用Tasks的应用程序。 我们还修改了cultureInfo(我们使用EN-US语言,但保留日期/数字格式),我们使用.Net 4.0。

应用程序有很multithreading和任务,我们有一个工厂来创建任务/线程。

对于该线程,我们有以下代码,以确保使用正确的CurrentCulture启动每个线程:

//This is basically only the constructor, but it describe well how we create the Thread: public MonitoredThread(ThreadStart threadStart, string name, bool isBackground = false) { m_threadStart = threadStart; m_name = name; m_isBackground = isBackground; Thread = new Thread(ThreadWorker) { Name = name, IsBackground = isBackground, CurrentCulture = CustomCultureInfo.CurrentCulture, CurrentUICulture = CustomCultureInfo.CurrentCulture }; } 

但是对于Tasks,我不知道如何实现这种机制:

 public static Task ExecuteTask(Action action, string name) { MonitoredTask task = new MonitoredTask(action, name); return Task.Factory.StartNew(task.TaskWorker); } 

任何的想法?

我不确定你真的需要一个MonitoredTask 。 您可以使用闭包捕获自定义文化:

 public static Task ExecuteTask(Action action, string name) { var customCulture = CustomCultureInfo.CurrentCulture; return Task.Factory.StartNew(() => { // use customCulture variable as needed // inside the generated task. }); } 

另一种方法是使用适当的重载( ActionFunc )将当前文化作为object state传递:

 public static Task ExecuteTask(Action action, string name) { var customCulture = CustomCultureInfo.CurrentCulture; return Task.Factory.StartNew((obj) => { var culture = (CultureInfo) obj; // use customCulture variable as needed // inside the generated task. }, customCulture); } 

我肯定会和前者一起去。

有关闭包的更多信息,请参阅.NET中的“闭包”是什么?

只是为@Yuval Itzchakov回答添加更多细节,我通常会为TaskFactory类创建一些保留Culture的扩展方法(我通常还会添加一个接收将任何给定属性设置为执行线程的操作的方法:

 #region StartNewWithPersistedCulture methods public static Task StartNewWithPersistedCulture( this TaskFactory taskFactory, Func function, CancellationToken cancellationToken = default (CancellationToken), TaskCreationOptions creationOptions = default (TaskCreationOptions)) { if (taskFactory == null) throw new ArgumentNullException("taskFactory"); if (function == null) throw new ArgumentNullException("function"); var currentCulture = Thread.CurrentThread.CurrentCulture; var currentUICulture = Thread.CurrentThread.CurrentUICulture; return taskFactory.StartNew( () => { Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentUICulture; return function(); }, cancellationToken, creationOptions, TaskScheduler.Default); } public static Task StartNewWithPersistedCulture( this TaskFactory taskFactory, Action action, CancellationToken cancellationToken = default (CancellationToken), TaskCreationOptions creationOptions = default (TaskCreationOptions)) { if (taskFactory == null) throw new ArgumentNullException("taskFactory"); if (action == null) throw new ArgumentNullException("action"); var currentCulture = Thread.CurrentThread.CurrentCulture; var currentUICulture = Thread.CurrentThread.CurrentUICulture; return taskFactory.StartNew( () => { Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentUICulture; action(); }, cancellationToken, creationOptions, TaskScheduler.Default); } #endregion