是否可以在工作线程中引发事件时更改主线程的 CurrentUICulture?
示例代码:
static void Main()
{
//do something
Thread workerThread = new Thread(new ThreadStart(DoWork));
workerThread.Start();
//do something
}
void DoWork()
{
ConnectDatabase();
//do some work
ChangeLanguage(lang);
}
void ChangeLanguage(string lang)
{
//this line changes Culture only for worker thread
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang);
}
Its WinForm client application and it has user settings stored in a database. Worker thread connects database (it takes some time). After successful connection, application should set language according to the user settings.
当您的 Thread 完成时,它可以使用 MainForm.Invoke(helperMethod)
然后 helperMethod 在主线程上运行并且可以安全地设置 Culture。
A backgroundworker会让事情变得更容易。它有一个在主线程上运行的 Completed 事件。
我是一名优秀的程序员,十分优秀!