CultureInfo.DefaultThreadCurrentCulture 属性

定义

获取或设置当前应用程序域中线程的默认区域性。

public:
 static property System::Globalization::CultureInfo ^ DefaultThreadCurrentCulture { System::Globalization::CultureInfo ^ get(); void set(System::Globalization::CultureInfo ^ value); };
public static System.Globalization.CultureInfo DefaultThreadCurrentCulture { get; set; }
public static System.Globalization.CultureInfo? DefaultThreadCurrentCulture { get; set; }
static member DefaultThreadCurrentCulture : System.Globalization.CultureInfo with get, set
Public Shared Property DefaultThreadCurrentCulture As CultureInfo

属性值

CultureInfo

如果当前系统区域性为应用程序域中的默认线程区域性,则为当前应用程序中线程的默认区域性或 null

示例

以下示例演示.NET Framework定义新线程的当前区域性时的默认行为。 在启动时,该示例在所有系统上将当前区域性和当前 UI 区域性设置为法语 (法国) ,但默认系统区域性已是法语 (法国) 。 如果默认系统区域性已是法语 (法国) ,则代码会将当前区域性和当前 UI 区域性设置为英语 (美国) 。 然后, DisplayRandomNumbers 它调用例程,该例程生成三个随机数并将其显示为货币值。 接下来,它会创建一个新线程,该线程也会执行例 DisplayRandomNumbers 程。

using System;
using System.Globalization;
using System.Text;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.OutputEncoding = Encoding.UTF8;
      // Change current culture
      CultureInfo culture;
      if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("fr-FR");

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      // Generate and display three random numbers on the current thread.
      DisplayRandomNumbers();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(new ThreadStart(Example.DisplayRandomNumbers));
      workerThread.Start();
   }

   private static void DisplayRandomNumbers()
   {
      Console.WriteLine();
      Console.WriteLine("Current Culture:    {0}",
                        Thread.CurrentThread.CurrentCulture);
      Console.WriteLine("Current UI Culture: {0}",
                        Thread.CurrentThread.CurrentUICulture);

      Console.Write("Random Values: ");
      Random rand = new Random();
      for (int ctr = 0; ctr <= 2; ctr++)
         Console.Write("     {0:C2}     ", rand.NextDouble());

      Console.WriteLine();
   }
}
// The example displays output similar to the following:
//    Current Culture:    fr-FR
//    Current UI Culture: fr-FR
//    Random Values:      0,77 €          0,35 €          0,52 €
//
//    Current Culture:    en-US
//    Current UI Culture: en-US
//    Random Values:      $0.30          $0.79          $0.65
Imports System.Globalization
Imports System.Text
Imports System.Threading

Module Example

   Public Sub Main()
      Console.OutputEncoding = Encoding.UTF8 
      ' Change current culture
      Dim culture As CultureInfo
      If Thread.CurrentThread.CurrentCulture.Name = "fr-FR" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("fr-FR")
      End If   
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture
      
      ' Generate and display three random numbers on the current thread.
      DisplayRandomNumbers()
      Thread.Sleep(1000)
      
      Dim workerThread As New Thread(AddressOf Example.DisplayRandomNumbers)
      workerThread.Start()
   End Sub
   
   Private Sub DisplayRandomNumbers()
      Console.WriteLine()
      Console.WriteLine("Current Culture:    {0}", 
                        Thread.CurrentThread.CurrentCulture)
      Console.WriteLine("Current UI Culture: {0}", 
                        Thread.CurrentThread.CurrentUICulture)

      Console.Write("Random Values: ")
      Dim rand As New Random()
      For ctr As Integer = 0 To 2
         Console.Write("     {0:C2}     ", rand.NextDouble())
      Next      
      Console.WriteLine()
   End Sub
End Module
' The example displays output similar to the following:
'    Current Culture:    fr-FR
'    Current UI Culture: fr-FR
'    Random Values:      0,77 €          0,35 €          0,52 €     
'    
'    Current Culture:    en-US
'    Current UI Culture: en-US
'    Random Values:      $0.30          $0.79          $0.65

如示例输出所示,当示例在系统区域性为英语 (美国) 的计算机上运行时,主线程使用法语 (法国) 区域性的格式设置约定显示其货币值。 但是,由于工作线程的区域性派生自当前 Windows 系统区域性而不是应用程序的当前区域性,因此工作线程使用英语 (美国) 区域性的格式设置约定显示其货币值。

以下示例使用 DefaultThreadCurrentCultureDefaultThreadCurrentUICulture 属性来定义新应用程序线程的当前区域性和当前 UI 区域性。 在启动时,该示例在所有系统上将当前区域性和当前 UI 区域性设置为法语 (法国) ,但默认系统区域性已是法语 (法国) 。 如果默认系统区域性已是法语 (法国) ,则会将当前区域性和当前 UI 区域性设置为英语 (美国) 。 然后, DisplayRandomNumbers 它调用例程,该例程生成三个随机数并将其显示为货币值。 接下来,它会创建一个新线程,该线程也会执行例 DisplayRandomNumbers 程。

using System;
using System.Globalization;
using System.Text;
using System.Threading;

public class Example
{
   public static void Main()
   {
      Console.OutputEncoding = Encoding.UTF8;
      // Change current culture
      CultureInfo culture;
      if (Thread.CurrentThread.CurrentCulture.Name == "fr-FR")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("fr-FR");

      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;

      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      // Generate and display three random numbers on the current thread.
      DisplayRandomNumbers();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(new ThreadStart(Example.DisplayRandomNumbers));
      workerThread.Start();
   }

   private static void DisplayRandomNumbers()
   {
      Console.WriteLine();
      Console.WriteLine("Current Culture:    {0}",
                        Thread.CurrentThread.CurrentCulture);
      Console.WriteLine("Current UI Culture: {0}",
                        Thread.CurrentThread.CurrentUICulture);

      Console.Write("Random Values: ");
      Random rand = new Random();
      for (int ctr = 0; ctr <= 2; ctr++)
         Console.Write("     {0:C2}     ", rand.NextDouble());

      Console.WriteLine();
   }
}
// The example displays output similar to the following:
//    Current Culture:    fr-FR
//    Current UI Culture: fr-FR
//    Random Values:      0,78 €          0,80 €          0,37 €
//
//    Current Culture:    fr-FR
//    Current UI Culture: fr-FR
//    Random Values:      0,52 €          0,32 €          0,15 €
Imports System.Globalization
Imports System.Text
Imports System.Threading

Module Example
   Public Sub Main()
      Console.OutputEncoding = Encoding.UTF8 
      ' Change current culture
      Dim culture As CultureInfo
      
      If Thread.CurrentThread.CurrentCulture.Name = "fr-FR" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("fr-FR")
      End If   
      CultureInfo.DefaultThreadCurrentCulture = culture
      CultureInfo.DefaultThreadCurrentUICulture = culture
      
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture
      
      ' Generate and display three random numbers on the current thread.
      DisplayRandomNumbers()
      Thread.Sleep(1000)
      
      Dim workerThread As New Thread(AddressOf Example.DisplayRandomNumbers)
      workerThread.Start()
   End Sub
   
   Private Sub DisplayRandomNumbers()
      Console.WriteLine()
      Console.WriteLine("Current Culture:    {0}", 
                        Thread.CurrentThread.CurrentCulture)
      Console.WriteLine("Current UI Culture: {0}", 
                        Thread.CurrentThread.CurrentUICulture)
      Console.Write("Random Values: ")
      Dim rand As New Random()
      For ctr As Integer = 0 To 2
         Console.Write("     {0:C2}     ", rand.NextDouble())
      Next      
      Console.WriteLine()
   End Sub
End Module
' The example displays output similar to the following:
'    Current Culture:    fr-FR
'    Current UI Culture: fr-FR
'    Random Values:      0,78 €          0,80 €          0,37 €
'    
'    Current Culture:    fr-FR
'    Current UI Culture: fr-FR
'    Random Values:      0,52 €          0,32 €          0,15 €

如示例输出所示,当示例在系统区域性为英语 (美国) 的计算机上运行时,主线程和工作线程都使用法语 (法国) 区域性的格式设置约定显示其货币值。

注解

在 .NET Framework 4 及早期版本中,默认情况下,所有线程的区域性都设置为 Windows 系统区域性。 对于当前区域性不同于默认系统区域性的应用程序,此行为通常是不可取的。 在 .NET Framework 4.5 中, DefaultThreadCurrentCulture 属性使应用程序能够定义应用程序域中所有线程的默认区域性。

重要

如果尚未显式设置在应用程序域中执行的任何现有线程的区域性,则设置 DefaultThreadCurrentCulture 属性也会更改这些线程的区域性。 但是,如果这些线程在另一个应用程序域中执行,则它们的区域性由 DefaultThreadCurrentCulture 该应用程序域中的 属性定义,如果未定义默认值,则由默认系统区域性定义。 因此,建议始终显式设置主应用程序线程的区域性,而不要依赖 DefaultThreadCurrentCulture 属性来定义主应用程序线程的区域性。

除非显式设置,否则 属性nullDefaultThreadCurrentCulture值为 ,并且应用程序域中尚未分配显式区域性的线程的区域性由默认 Windows 系统区域性定义。

有关区域性、线程和应用程序域的详细信息,请参阅参考页中的 CultureInfo “区域性和线程”和“区域性和应用程序域”部分。

适用于

另请参阅