CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentCulture Property

定义

获取或设置当前应用程序域中线程的默认区域性。Gets or sets the default culture for threads in the current application domain.

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; }
member this.DefaultThreadCurrentCulture : System.Globalization.CultureInfo with get, set
Public Shared Property DefaultThreadCurrentCulture As CultureInfo

属性值

如果当前系统区域性为应用程序域中的默认线程区域性,则为当前应用程序中线程的默认区域性或 nullThe default culture for threads in the current application domain, or null if the current system culture is the default thread culture in the application domain.

示例

下面的示例演示了定义新线程的当前区域性时 .NET Framework 的默认行为。The following example illustrates the default behavior of the .NET Framework in defining the current culture of a new thread. 在启动时,该示例将当前区域性和当前 UI 区域性设置为法语(法国),所有系统上的默认系统区域性都为法语(法国)。At startup, the example sets the current culture and the current UI culture to French (France) on all systems except those on which the default system culture is already French (France). 如果默认系统区域性已为 "法语(法国)",则代码会将当前区域性和当前 UI 区域性设置为 "英语(美国)"。If the default system culture is already French (France), the code sets the current culture and the current UI culture to English (United States). 然后,它调用DisplayRandomNumbers例程,该例程生成三个随机数并将它们显示为货币值。It then calls the DisplayRandomNumbers routine, which generates three random numbers and displays them as currency values. 接下来,它创建一个新的线程,该线程DisplayRandomNumbers也执行例程。Next, it creates a new thread, which also executes the DisplayRandomNumbers routine.

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     

如示例中的输出所示,当在系统区域性为英语(美国)的计算机上运行该示例时,主线程将使用法语(法国)区域性的格式设置约定来显示其货币值。As the output from the example shows, when the example is run on a computer whose system culture is English (United States), the main thread displays its currency values using the formatting conventions of the French (France) culture. 但是,因为工作线程的区域性派生自当前的 Windows 系统区域性而不是应用程序的当前区域性,所以,工作线程使用英语(美国)区域性的格式设置约定来显示其货币值。However, because the worker thread's culture is derived from the current Windows system culture rather than the application's current culture, the work thread displays its currency values using the formatting conventions of the English (United States) culture.

下面的示例使用DefaultThreadCurrentCultureDefaultThreadCurrentUICulture属性定义新的应用程序线程的当前区域性和当前 UI 区域性。The following example uses the DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties to define the current culture and current UI culture of a new application thread. 在启动时,该示例将当前区域性和当前 UI 区域性设置为法语(法国),所有系统上的默认系统区域性都为法语(法国)。At startup, the example sets the current culture and the current UI culture to French (France) on all systems except those on which the default system culture is already French (France). 如果默认系统区域性为 "法语(法国)",则它将当前区域性和当前 UI 区域性设置为 "英语(美国)"。If the default system culture is already French (France), it sets the current culture and the current UI culture to English (United States). 然后,它调用DisplayRandomNumbers例程,该例程生成三个随机数并将它们显示为货币值。It then calls the DisplayRandomNumbers routine, which generates three random numbers and displays them as currency values. 接下来,它创建一个新的线程,该线程DisplayRandomNumbers也执行例程。Next, it creates a new thread, which also executes the DisplayRandomNumbers routine.

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 €

如示例中的输出所示,当在系统区域性为英语(美国)的计算机上运行该示例时,主线程和工作线程均使用法语(法国)区域性的格式设置约定来显示它们的货币值。As the output from the example shows, when the example is run on a computer whose system culture is English (United States), both the main thread and the worker thread display their currency values using the formatting conventions of the French (France) culture.

注解

.NET Framework 4.NET Framework 4在和早期版本中,默认情况下,所有线程的区域性都设置为 Windows 系统区域性。In the .NET Framework 4.NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture. 对于当前区域性与默认系统区域性不同的应用程序,通常不需要此行为。For applications whose current culture differs from the default system culture, this behavior is often undesirable. 在中.NET Framework 4.5.NET Framework 4.5 DefaultThreadCurrentCulture ,属性使应用程序能够定义应用程序域中所有线程的默认区域性。In the .NET Framework 4.5.NET Framework 4.5, the DefaultThreadCurrentCulture property enables an application to define the default culture of all threads in an application domain.

重要

如果未显式设置在应用程序域中执行的任何现有线程的区域性,则设置DefaultThreadCurrentCulture属性还会更改这些线程的区域性。If you have not explicitly set the culture of any existing threads executing in an application domain, setting the DefaultThreadCurrentCulture property also changes the culture of these threads. 但是,如果这些线程在另一个应用程序域中执行,则其区域性DefaultThreadCurrentCulture由该应用程序域中的属性定义,或者,如果未定义默认值,则由默认系统区域性定义。However, if these threads execute in another application domain, their culture is defined by the DefaultThreadCurrentCulture property in that application domain or, if no default value is defined, by the default system culture. 因此,建议你始终显式设置主应用程序线程的区域性,而不是DefaultThreadCurrentCulture依赖于属性来定义主应用程序线程的区域性。Because of this, we recommend that you always explicitly set the culture of your main application thread, and not rely on the DefaultThreadCurrentCulture property to define the culture of the main application thread.

除非显式设置,否则DefaultThreadCurrentCulture属性的值为null,并且应用程序域中未分配显式区域性的线程的区域性是由默认的 Windows 系统区域性定义的。Unless it is set explicitly, the value of the DefaultThreadCurrentCulture property is null, and the culture of threads in an application domain that have not been assigned an explicit culture is defined by the default Windows system culture.

有关区域性、线程和应用程序域的详细信息,请参阅 " CultureInfo引用" 页中的 "区域性和线程" 和 "区域性和应用程序域" 部分。For more information about cultures, threads, and application domains, see the "Culture and threads" and "Culture and application domains" sections in the CultureInfo reference page.

适用于

另请参阅