gpt4 book ai didi

c# - 从工作线程更改主线程的 CultureInfo

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:29 24 4
gpt4 key购买 nike

是否可以在工作线程中引发事件时更改主线程的 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 事件。

关于c# - 从工作线程更改主线程的 CultureInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3297161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com