Windows Develop Bookmark and Share   
 index > Windows Forms General > How set CurrentUICulture in the backgroundworker thread?
 

How set CurrentUICulture in the backgroundworker thread?

I have problem with backgroundworker control. In backgroundworker's thread I'm reading resource text. If windows regional settings (e.g. German) are not same as application's language (e.g. English) wrong language will be readed (German). I suppose that background worker component reads CurrentUICulture from Windows.

How to correctly solve this issue? My idea is to set backgroundworker's thread UI culture in DoWork event handler to application's language. How to do that?

Thank you
Anonymous456465  Wednesday, August 20, 2008 2:00 PM
Surround your processing with code that changes the CurrentUICulture. Since BackgroundWorker uses a thread pool, I would recommend using a try/finally to leave things the way you found them.

In DoWork:

Code Snippet

System.Globalization.CultureInfo oldCulture =

System.Threading.Thread.CurrentThread.CurrentUICulture;

try

{

System.Threading.Thread.CurrentThread.CurrentUICulture =

new System.Globalization.CultureInfo("en-US");

// Your processing here.

}

finally

{

System.Threading.Thread.CurrentThread.CurrentUICulture = oldCulture;

}

BinaryCoder  Thursday, August 21, 2008 12:00 AM
Don't forget Thread.CurrentCulture, pretty important since BGW can't touch the UI.
nobugz  Saturday, August 23, 2008 1:45 PM
Surround your processing with code that changes the CurrentUICulture. Since BackgroundWorker uses a thread pool, I would recommend using a try/finally to leave things the way you found them.

In DoWork:

Code Snippet

System.Globalization.CultureInfo oldCulture =

System.Threading.Thread.CurrentThread.CurrentUICulture;

try

{

System.Threading.Thread.CurrentThread.CurrentUICulture =

new System.Globalization.CultureInfo("en-US");

// Your processing here.

}

finally

{

System.Threading.Thread.CurrentThread.CurrentUICulture = oldCulture;

}

BinaryCoder  Thursday, August 21, 2008 12:00 AM
Don't forget Thread.CurrentCulture, pretty important since BGW can't touch the UI.
nobugz  Saturday, August 23, 2008 1:45 PM
BGW can't touch the UI but may use localized resources which are picked using the UI Culture so both cultures should be set
juanagui  Wednesday, August 12, 2009 2:45 PM

You can use google to search for other answers

Custom Search

More Threads

• WebBrowser control always scrolling HTML doc back to top
• RichTextBox & Multilines selection tab
• SqlClient error
• Datacolumn Expression values from CR
• RichTextBox
• Control.Invoke/BeginInvoke Deadlock
• adding custom text box and combobox into specific row/cell of datagridview
• How do I set a ComboBox to ReadOnly?
• SendKeys and Bell
• Spell check in c# using Word spell checkeer