From fcd4961d285dfe1eed6474577d77903cfd71e926 Mon Sep 17 00:00:00 2001 From: soukoku Date: Tue, 15 Apr 2014 18:57:03 -0400 Subject: [PATCH] Fixed message loop thread may not start until too late (#6). --- NTwain/MessageLoop.cs | 8 ++++++++ NTwain/Properties/VersionInfo.cs | 2 +- Tests/Tester.Winform/TestForm.cs | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/NTwain/MessageLoop.cs b/NTwain/MessageLoop.cs index ccd660f..8dfd419 100644 --- a/NTwain/MessageLoop.cs +++ b/NTwain/MessageLoop.cs @@ -1,6 +1,7 @@ using NTwain.Triplets; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; @@ -27,8 +28,12 @@ namespace NTwain { if (!_started) { + // using this terrible hack so the new thread will start running before this function returns + var hack = new ManualResetEvent(false); + var loopThread = new Thread(new ThreadStart(() => { + Debug.WriteLine("NTwain message loop started."); _dispatcher = Dispatcher.CurrentDispatcher; if (Dsm.IsWin) { @@ -37,11 +42,14 @@ namespace NTwain // CS_NOCLOSE, WS_DISABLED, and WS_EX_NOACTIVATE _dummyWindow = new HwndSource(0x0200, 0x8000000, 0x8000000, 0, 0, "NTWAIN_LOOPER", IntPtr.Zero); } + hack.Set(); Dispatcher.Run(); })); loopThread.IsBackground = true; loopThread.SetApartmentState(ApartmentState.STA); loopThread.Start(); + hack.WaitOne(); + hack.Close(); _started = true; } } diff --git a/NTwain/Properties/VersionInfo.cs b/NTwain/Properties/VersionInfo.cs index 98bb8b4..7a36978 100644 --- a/NTwain/Properties/VersionInfo.cs +++ b/NTwain/Properties/VersionInfo.cs @@ -14,6 +14,6 @@ namespace NTwain // keep this same in majors releases public const string Release = "0.11.0.0"; // change this for each nuget release - public const string Build = "0.11.0"; + public const string Build = "0.11.1"; } } \ No newline at end of file diff --git a/Tests/Tester.Winform/TestForm.cs b/Tests/Tester.Winform/TestForm.cs index ea5064c..56dd04b 100644 --- a/Tests/Tester.Winform/TestForm.cs +++ b/Tests/Tester.Winform/TestForm.cs @@ -42,7 +42,6 @@ namespace Tester.Winform { if (enc.MimeType == "image/tiff") { _tiffCodecInfo = enc; break; } } - SetupTwain(); } protected override void OnFormClosing(FormClosingEventArgs e) @@ -231,6 +230,10 @@ namespace Tester.Winform private void ReloadSourceList() { + if (_twain == null) + { + SetupTwain(); + } if (_twain.State < 3) { _twain.OpenManager();