I've an application where we use Tasks. We also modified the cultureInfo(we use the EN-US language, but keep the date/number format), and we use .Net 4.0.
The application has a lot of thread and task, and we have a factory for the creation of Task/Threads.
For the thread, we have the following code, to ensure that every thread is launched with the correct 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
};
}
But for the Tasks, I don't know how to implement this kind of mechanism:
public static Task ExecuteTask(Action action, string name)
{
MonitoredTask task = new MonitoredTask(action, name);
return Task.Factory.StartNew(task.TaskWorker);
}
Any idea?
TaskWorker
? – MarvamarveMonitoredTask
is a custom class representing aTask
(we trace them when they starts, when they finish, etc...TaskWorker
is a property giving access to theTask
mapped by this class – InstaurationTaskWorker
is the Action that we gave in parameter of the constructor. – Instauration