直接上代码
新建一dll工程
代码如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ClassLibrary1
{
public class Class1
{
public delegate void Test();
public Test test;
public Class1()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(test1));
} private void test1(object sender)
{
while (true)
{
Thread.Sleep(10000);
if (test != null)
test();
}
} public void test2()
{
if (test != null)
test();
}
}
}
然后建立控制台程序,添加资源文件Localize.resx,添加字符串String1值:测试;在添加英文资源Localize.en.resx添加字符串String1值:Test.
控制台程序代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Resources;namespace ConsoleApplication1
{
class Program
{
private static ResourceManager m_resourceManager = new ResourceManager("ConsoleApplication1.Localize", System.Reflection.Assembly.GetExecutingAssembly());
static void Main(string[] args)
{
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en");
ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
c1.test = new ClassLibrary1.Class1.Test(test); c1.test2(); Console.ReadLine();
c1.test2(); Console.ReadLine();
} static void test()
{
Console.WriteLine(m_resourceManager.GetString("String1"));
}
}
}
运行后直接调用c1.test2()打印出来的字符串为英文,而ClassLibrary1十秒运行一次的test1打印出来为中文,请问有什么办法解决?
新建一dll工程
代码如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ClassLibrary1
{
public class Class1
{
public delegate void Test();
public Test test;
public Class1()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(test1));
} private void test1(object sender)
{
while (true)
{
Thread.Sleep(10000);
if (test != null)
test();
}
} public void test2()
{
if (test != null)
test();
}
}
}
然后建立控制台程序,添加资源文件Localize.resx,添加字符串String1值:测试;在添加英文资源Localize.en.resx添加字符串String1值:Test.
控制台程序代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Resources;namespace ConsoleApplication1
{
class Program
{
private static ResourceManager m_resourceManager = new ResourceManager("ConsoleApplication1.Localize", System.Reflection.Assembly.GetExecutingAssembly());
static void Main(string[] args)
{
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en");
ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
c1.test = new ClassLibrary1.Class1.Test(test); c1.test2(); Console.ReadLine();
c1.test2(); Console.ReadLine();
} static void test()
{
Console.WriteLine(m_resourceManager.GetString("String1"));
}
}
}
运行后直接调用c1.test2()打印出来的字符串为英文,而ClassLibrary1十秒运行一次的test1打印出来为中文,请问有什么办法解决?
ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
c1.test = new ClassLibrary1.Class1.Test(test);每次运行前,实例化一次 en
把这个写在委托里面。
Thread.CurrentThread.CurrentUICulture 这明明就是对当前线程进行的设置