CultureInfo.DefaultThreadCurrentCulture 屬性

定義

取得或設定目前應用程式定義域中之執行緒的預設文化特性。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; }
public static System.Globalization.CultureInfo? DefaultThreadCurrentCulture { get; set; }
member this.DefaultThreadCurrentCulture : System.Globalization.CultureInfo with get, set
Public Shared Property DefaultThreadCurrentCulture As CultureInfo

屬性值

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 和先前的版本中,所有線程的文化特性預設都設定為 Windows 系統文化特性。In the .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 中, DefaultThreadCurrentCulture 屬性可讓應用程式定義應用程式域中所有線程的預設文化特性。In the .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.

除非明確設定,否則屬性的值 DefaultThreadCurrentCulturenull ,而且尚未指派明確文化特性的應用程式域中之執行緒的文化特性是由預設的 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.

如需有關文化特性、執行緒和應用程式域的詳細資訊,請參閱參考頁面中的「文化特性和執行緒」和「文化特性和應用程式域」章節 CultureInfoFor more information about cultures, threads, and application domains, see the "Culture and threads" and "Culture and application domains" sections in the CultureInfo reference page.

適用於

另請參閱