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 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;
}
}

View File

@@ -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";
}
}

View File

@@ -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();