Fix culture for non-main threads

This commit is contained in:
Ben Olden-Cooligan 2015-12-21 14:01:17 -05:00
parent e22c73655b
commit df539dbfa9
7 changed files with 44 additions and 10 deletions

View File

@ -217,6 +217,7 @@
<Compile Include="Scan\Wia\WiaState.cs" />
<Compile Include="Scan\Wia\WinFormsWiaTransfer.cs" />
<Compile Include="Util\StringWrapper.cs" />
<Compile Include="Util\ThreadFactory.cs" />
<Compile Include="Util\Unmanaged.cs" />
<Compile Include="Util\UnmanagedArray.cs" />
<Compile Include="Util\UnmanagedBase.cs" />

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using NAPS2.Util;
namespace NAPS2.Scan.Wia
{
@ -19,12 +20,12 @@ namespace NAPS2.Scan.Wia
private Form form;
private WiaState wiaState;
public WiaBackgroundEventLoop(ScanProfile profile, ScanDevice scanDevice)
public WiaBackgroundEventLoop(ScanProfile profile, ScanDevice scanDevice, ThreadFactory threadFactory)
{
this.profile = profile;
this.scanDevice = scanDevice;
thread = new Thread(RunEventLoop);
thread = threadFactory.CreateThread(RunEventLoop);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
// Wait for the thread to initialize the background form and event loop

View File

@ -25,6 +25,7 @@ using System.Linq;
using System.Runtime.InteropServices;
using NAPS2.Scan.Exceptions;
using NAPS2.Scan.Images;
using NAPS2.Util;
namespace NAPS2.Scan.Wia
{
@ -34,11 +35,13 @@ namespace NAPS2.Scan.Wia
private readonly IScannedImageFactory scannedImageFactory;
private readonly IWiaTransfer wiaTransfer;
private readonly ThreadFactory threadFactory;
public WiaScanDriver(IScannedImageFactory scannedImageFactory, IWiaTransfer wiaTransfer)
public WiaScanDriver(IScannedImageFactory scannedImageFactory, IWiaTransfer wiaTransfer, ThreadFactory threadFactory)
{
this.scannedImageFactory = scannedImageFactory;
this.wiaTransfer = wiaTransfer;
this.threadFactory = threadFactory;
}
public override string DriverName
@ -53,7 +56,7 @@ namespace NAPS2.Scan.Wia
protected override IEnumerable<IScannedImage> ScanInternal()
{
using (var eventLoop = new WiaBackgroundEventLoop(ScanProfile, ScanDevice))
using (var eventLoop = new WiaBackgroundEventLoop(ScanProfile, ScanDevice, threadFactory))
{
bool supportsFeeder = eventLoop.GetSync(wia => WiaApi.DeviceSupportsFeeder(wia.Device));
if (ScanProfile.PaperSource != ScanSource.Glass && !supportsFeeder)

View File

@ -18,7 +18,7 @@ namespace NAPS2.Util
this.appConfigManager = appConfigManager;
}
public void InitCulture()
public void InitCulture(Thread thread)
{
var cultureId = userConfigManager.Config.Culture ?? appConfigManager.Config.DefaultCulture;
if (!String.IsNullOrWhiteSpace(cultureId))
@ -26,8 +26,8 @@ namespace NAPS2.Util
try
{
var culture = new CultureInfo(cultureId);
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
thread.CurrentUICulture = culture;
thread.CurrentCulture = culture;
}
catch (CultureNotFoundException e)
{

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace NAPS2.Util
{
public class ThreadFactory
{
private readonly CultureInitializer cultureInitializer;
public ThreadFactory(CultureInitializer cultureInitializer)
{
this.cultureInitializer = cultureInitializer;
}
public Thread CreateThread(ThreadStart threadStart)
{
// Using CultureInfo.DefaultThreadCurrentCulture would be eaiser, but it's .NET 4.5 only
var thread = new Thread(threadStart);
cultureInitializer.InitCulture(thread);
return thread;
}
}
}

View File

@ -28,6 +28,7 @@ using NAPS2.Config;
using NAPS2.ImportExport.Pdf;
using NAPS2.Lang.Resources;
using NAPS2.Scan.Images;
using NAPS2.Util;
namespace NAPS2.WinForms
{
@ -36,13 +37,15 @@ namespace NAPS2.WinForms
private readonly PdfSaver pdfSaver;
private readonly IUserConfigManager userConfigManager;
private readonly PdfSettingsContainer pdfSettingsContainer;
private readonly ThreadFactory threadFactory;
public FPdfSave(PdfSaver pdfSaver, IUserConfigManager userConfigManager, PdfSettingsContainer pdfSettingsContainer)
public FPdfSave(PdfSaver pdfSaver, IUserConfigManager userConfigManager, PdfSettingsContainer pdfSettingsContainer, ThreadFactory threadFactory)
{
InitializeComponent();
this.pdfSaver = pdfSaver;
this.userConfigManager = userConfigManager;
this.pdfSettingsContainer = pdfSettingsContainer;
this.threadFactory = threadFactory;
RestoreFormState = false;
Shown += FPDFSave_Shown;
}
@ -67,7 +70,7 @@ namespace NAPS2.WinForms
void FPDFSave_Shown(object sender, EventArgs e)
{
new Thread(ExportPdfProcess).Start();
threadFactory.CreateThread(ExportPdfProcess).Start();
}
public void SetStatus(int count, int total)

View File

@ -38,7 +38,7 @@ namespace NAPS2
[STAThread]
static void Main()
{
KernelManager.Kernel.Get<CultureInitializer>().InitCulture();
KernelManager.Kernel.Get<CultureInitializer>().InitCulture(Thread.CurrentThread);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);