mirror of
https://github.com/cyanfish/naps2.git
synced 2024-10-04 11:27:08 +03:00
Fix culture for non-main threads
This commit is contained in:
parent
e22c73655b
commit
df539dbfa9
@ -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" />
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
{
|
||||
|
26
NAPS2.Core/Util/ThreadFactory.cs
Normal file
26
NAPS2.Core/Util/ThreadFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user