Fixed message loop thread may not start until too late (#6).

This commit is contained in:
soukoku
2014-04-15 18:57:03 -04:00
parent af40d11103
commit fcd4961d28
3 changed files with 13 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
using NTwain.Triplets; using NTwain.Triplets;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -27,8 +28,12 @@ namespace NTwain
{ {
if (!_started) 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(() => var loopThread = new Thread(new ThreadStart(() =>
{ {
Debug.WriteLine("NTwain message loop started.");
_dispatcher = Dispatcher.CurrentDispatcher; _dispatcher = Dispatcher.CurrentDispatcher;
if (Dsm.IsWin) if (Dsm.IsWin)
{ {
@@ -37,11 +42,14 @@ namespace NTwain
// CS_NOCLOSE, WS_DISABLED, and WS_EX_NOACTIVATE // CS_NOCLOSE, WS_DISABLED, and WS_EX_NOACTIVATE
_dummyWindow = new HwndSource(0x0200, 0x8000000, 0x8000000, 0, 0, "NTWAIN_LOOPER", IntPtr.Zero); _dummyWindow = new HwndSource(0x0200, 0x8000000, 0x8000000, 0, 0, "NTWAIN_LOOPER", IntPtr.Zero);
} }
hack.Set();
Dispatcher.Run(); Dispatcher.Run();
})); }));
loopThread.IsBackground = true; loopThread.IsBackground = true;
loopThread.SetApartmentState(ApartmentState.STA); loopThread.SetApartmentState(ApartmentState.STA);
loopThread.Start(); loopThread.Start();
hack.WaitOne();
hack.Close();
_started = true; _started = true;
} }
} }

View File

@@ -14,6 +14,6 @@ namespace NTwain
// keep this same in majors releases // keep this same in majors releases
public const string Release = "0.11.0.0"; public const string Release = "0.11.0.0";
// change this for each nuget release // change this for each nuget release
public const string Build = "0.11.0"; public const string Build = "0.11.1";
} }
} }

View File

@@ -42,7 +42,6 @@ namespace Tester.Winform
{ {
if (enc.MimeType == "image/tiff") { _tiffCodecInfo = enc; break; } if (enc.MimeType == "image/tiff") { _tiffCodecInfo = enc; break; }
} }
SetupTwain();
} }
protected override void OnFormClosing(FormClosingEventArgs e) protected override void OnFormClosing(FormClosingEventArgs e)
@@ -231,6 +230,10 @@ namespace Tester.Winform
private void ReloadSourceList() private void ReloadSourceList()
{ {
if (_twain == null)
{
SetupTwain();
}
if (_twain.State < 3) if (_twain.State < 3)
{ {
_twain.OpenManager(); _twain.OpenManager();