संपादित करें

इसके माध्यम से साझा किया गया


CultureInfo.DefaultThreadCurrentUICulture Property

Definition

Gets or sets the default UI culture for threads in the current application domain.

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

Property Value

The default UI culture for threads in the current application domain, or null if the current system UI culture is the default thread UI culture in the application domain.

Exceptions

In a set operation, the Name property value is invalid.

Examples

The following example illustrates the default behavior of the .NET Framework in defining the current culture of a new thread. It uses English and Russian language resources. The following text file named GreetingStrings.txt contains the English language resources:

greeting =Hello again!
newGreeting=Hello!

It is compiled to a binary .resources file named GreetingStrings.resources by using the Resource File Generator with the following command.

resgen greetingstrings.txt

The following text file named GreetingStrings.ru-RU.txt contains the Russian language resources:

greeting=Еще раз привет!
newGreeting=Привет!

It is compiled to a binary .resources file named GreetingStrings.ru-RU.resources by using the Resource File Generator with the following command.

resgen greetingstrings.ru-RU.txt

The application code, which is shown below, resides in a file named Example1.vb or Example1.cs. It is compiled to an executable by using the following command for the Visual Basic compiler:

vbc Example1.vb /resource:GreetingStrings.resources

For the C# compiler, the command is similar:

csc /resource:GreetingStrings.resources Example1.cs

This creates an assembly that includes the example's executable code along with the resources for its fallback culture. You can also use the Assembly Linker to create the resource file for the Russian (Russia) culture with the following command:

>al /embed:greetingstrings.ru-RU.resources /c:ru-RU /template:example1.exe /out:ru-RU\Example1.resources.dll

At startup, the example sets the current culture and the current UI culture to Russian (Russia) on all systems except those on which the default system culture is already Russian (Russia). If the default system culture is already Russian (Russia), the code sets the current culture and the current UI culture to English (United States). It then calls the ShowGreeting routine, which displays a simple string the first time it is called and a slightly different string on subsequent method calls. Next, it creates a new thread, which also executes the ShowGreeting routine.

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

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

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Hello again!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Hello again!

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 initial string in Russian. However, because the worker thread's culture is derived from the current Windows system culture rather than the application's current culture, the worker thread displays its string in English.

The following example uses the DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture properties to define the current culture and current UI culture of a new application thread.

The example uses the same resources files as the previous example. The commands to compile and to embed the localized Russian language resources into a satellite assembly are also identical, except that the name of the executable assembly changes.

At startup, the example sets the current culture and the current UI culture to Russian (Russia) on all systems except those on which the default system culture is already Russian (Russia). If the default system culture is already Russian (Russia), it sets the current culture and the current UI culture to English (United States). It then calls the ShowGreeting routine, which displays a simple string the first time it is called and a slightly different string on subsequent method calls. Next, it creates a new thread, which also executes the ShowGreeting routine.

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

[assembly:NeutralResourcesLanguageAttribute("en-US")]

public class Example
{
   private static int nGreetings = 0;
   private static ResourceManager rm;

   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      rm = new ResourceManager("GreetingStrings",
                               typeof(Example).Assembly);

      CultureInfo culture = null;
      if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
         culture = CultureInfo.CreateSpecificCulture("en-US");
      else
         culture = CultureInfo.CreateSpecificCulture("ru-RU");

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

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

      ShowGreeting();
      Thread.Sleep(1000);

      Thread workerThread = new Thread(Example.ShowGreeting);
      workerThread.Start();
   }

   private static void ShowGreeting()
   {
      string greeting = nGreetings == 0 ? rm.GetString("newGreeting") :
                                          rm.GetString("greeting");
      nGreetings++;
      Console.WriteLine("{0}", greeting);
   }
}
// The example displays the following output:
//       Привет!
//       Еще раз привет!
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Threading

<Assembly:NeutralResourcesLanguageAttribute("en-US")>

Public Class Example
   Private Shared nGreetings As Integer = 0
   Private Shared rm As ResourceManager

   Public Shared Sub Main()
      Dim domain As AppDomain = AppDomain.CurrentDomain
      rm = New ResourceManager("GreetingStrings", 
                               GetType(Example).Assembly)
                  
      Dim culture As CultureInfo = Nothing
      If Thread.CurrentThread.CurrentUICulture.Name = "ru-RU" Then
         culture = CultureInfo.CreateSpecificCulture("en-US")
      Else
         culture = CultureInfo.CreateSpecificCulture("ru-RU")
      End If   
      CultureInfo.DefaultThreadCurrentCulture = culture
      CultureInfo.DefaultThreadCurrentUICulture = culture
       
      Thread.CurrentThread.CurrentCulture = culture
      Thread.CurrentThread.CurrentUICulture = culture

      ShowGreeting()
      Thread.Sleep(1000)

      Dim workerThread As New Thread(AddressOf Example.ShowGreeting)
      workerThread.Start()
   End Sub
   
   Private Shared Sub ShowGreeting()
      Dim greeting As String = CStr(IIf(nGreetings = 0, 
                                        rm.GetString("newGreeting"),
                                        rm.GetString("greeting")))
      nGreetings += 1
      Console.WriteLine("{0}", greeting)   
   End Sub
End Class
' The example displays the following output:
'       Привет!
'       Еще раз привет!

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 strings in the Russian language.

Remarks

In the .NET Framework 4 and previous versions, by default, the UI culture of all threads is set to the Windows system culture. For applications whose current UI culture differs from the default system culture, this behavior is often undesirable. In the .NET Framework 4.5, the DefaultThreadCurrentUICulture property lets you define the default UI culture of all threads in an application domain.

Important

If you have not explicitly set the UI culture of any existing threads executing in an application domain, setting the DefaultThreadCurrentUICulture property also changes the culture of these threads. However, if these threads execute in another application domain, their culture is defined by the DefaultThreadCurrentUICulture property in that application domain or, if no default value is defined, by the default system culture. Because of this, we recommend that you always explicitly set the culture of your main application thread and do not rely on the DefaultThreadCurrentUICulture property to define the culture of the main application thread.

Unless it is set explicitly, the value of the DefaultThreadCurrentUICulture property is null, and the current culture of all threads in an application domain that have not been assigned an explicit culture is defined by the default Windows system culture.

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.

Applies to

See also