From 3ae070e3a376f5f891ddd220b3d7ab989d034bb5 Mon Sep 17 00:00:00 2001 From: soukoku Date: Mon, 14 Apr 2014 19:51:36 -0400 Subject: [PATCH] More update for internal loop handling. --- NTwain/Properties/VersionInfo.cs | 4 +-- NTwain/TwainSession.cs | 61 +++++++++++++++++--------------- README.markdown | 11 +++--- Tests/Tester.Console/Program.cs | 2 +- Tests/Tester.Winform/TestForm.cs | 2 +- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/NTwain/Properties/VersionInfo.cs b/NTwain/Properties/VersionInfo.cs index f7bc74d..98bb8b4 100644 --- a/NTwain/Properties/VersionInfo.cs +++ b/NTwain/Properties/VersionInfo.cs @@ -12,8 +12,8 @@ namespace NTwain class _NTwainVersionInfo { // keep this same in majors releases - public const string Release = "0.10.0.0"; + public const string Release = "0.11.0.0"; // change this for each nuget release - public const string Build = "0.10.2"; + public const string Build = "0.11.0"; } } \ No newline at end of file diff --git a/NTwain/TwainSession.cs b/NTwain/TwainSession.cs index 23b867d..46cccc9 100644 --- a/NTwain/TwainSession.cs +++ b/NTwain/TwainSession.cs @@ -32,8 +32,6 @@ namespace NTwain ((ITwainStateInternal)this).ChangeState(1, false); EnforceState = true; - Debug.WriteLine("current thread id =" + Thread.CurrentThread.ManagedThreadId); - MessageLoop.Instance.EnsureStarted(); } @@ -298,8 +296,6 @@ namespace NTwain rc = DGControl.Identity.CloseDS(); if (rc == ReturnCode.Success) { - MessageLoop.Instance.RemoveHook(HandleWndProcMessage); - _callbackObj = null; SupportedCaps = null; } }); @@ -385,6 +381,14 @@ namespace NTwain rc = DGControl.UserInterface.DisableDS(_twui); if (rc == ReturnCode.Success) { + if (_callbackObj == null) + { + MessageLoop.Instance.RemoveHook(HandleWndProcMessage); + } + else + { + _callbackObj = null; + } OnSourceDisabled(); } }); @@ -418,26 +422,29 @@ namespace NTwain // success, the return status from DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER may // be ignored. - if (targetState < 7) + MessageLoop.Instance.Invoke(() => { - DGControl.PendingXfers.EndXfer(new TWPendingXfers()); - } - if (targetState < 6) - { - DGControl.PendingXfers.Reset(new TWPendingXfers()); - } - if (targetState < 5) - { - DisableSource(); - } - if (targetState < 4) - { - CloseSource(); - } - if (targetState < 3) - { - CloseManager(); - } + if (targetState < 7) + { + DGControl.PendingXfers.EndXfer(new TWPendingXfers()); + } + if (targetState < 6) + { + DGControl.PendingXfers.Reset(new TWPendingXfers()); + } + if (targetState < 5) + { + DisableSource(); + } + if (targetState < 4) + { + CloseSource(); + } + if (targetState < 3) + { + CloseManager(); + } + }); EnforceState = origFlag; } @@ -601,12 +608,10 @@ namespace NTwain IntPtr HandleWndProcMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // this handles the message from a typical WndProc message loop and check if it's from the TWAIN source. - - var winmsg = new MESSAGE(hwnd, msg, wParam, lParam); - - if (State >= 4) // technically we should only handle on state >= 5 but there might be missed msgs if we wait until state changes after enabling ds + if (State >= 5) { // transform it into a pointer for twain + var winmsg = new MESSAGE(hwnd, msg, wParam, lParam); IntPtr msgPtr = IntPtr.Zero; try { @@ -614,7 +619,7 @@ namespace NTwain msgPtr = Marshal.AllocHGlobal(Marshal.SizeOf(winmsg)); Marshal.StructureToPtr(winmsg, msgPtr, false); - TWEvent evt = new TWEvent(); + var evt = new TWEvent(); evt.pEvent = msgPtr; if (handled = (DGControl.Event.ProcessEvent(evt) == ReturnCode.DSEvent)) { diff --git a/README.markdown b/README.markdown index a8328bc..6fa9ec6 100644 --- a/README.markdown +++ b/README.markdown @@ -4,13 +4,13 @@ TWAIN Application-Side Library Info -------------------------------------- This is a dotnet library created to work with [TWAIN](http://twain.org/) interface. -This project follows these general goals: +This project has these features: * Targets latest TWAIN version (2.3 at the moment) * Supports all the TWAIN functions in the spec -* Eventally work on platforms other than Windows (I can dream) +* Hosts internal message loop so no need to hook into application UI thread -The solution contains sample projects in winforms, wpf, and even (gasp!) console. +The solution contains tester projects in winform, wpf, and even (gasp!) console. A nuget package is also [available here](https://www.nuget.org/packages/ntwain) Using the lib @@ -22,4 +22,7 @@ when and how to use them. There are no high-level, single-line scan-a-page-for-m At the moment this lib does not provide ways to parse transferred image data and require consumers to do the conversion. The winform project contains one such -example for handling DIB image in native transfer. \ No newline at end of file +example for handling DIB image in native transfer. + +The main class to use is TwainSession. New it up, hook into the events, and start calling +all the TWAIN functions provided through it. \ No newline at end of file diff --git a/Tests/Tester.Console/Program.cs b/Tests/Tester.Console/Program.cs index 3b38844..7e13bfe 100644 --- a/Tests/Tester.Console/Program.cs +++ b/Tests/Tester.Console/Program.cs @@ -19,7 +19,7 @@ namespace Tester { // just an amusing example to do twain in console without UI DoTwainWork(); - Console.WriteLine("Test completed, press Enter to exit."); + Console.WriteLine("Test started, press Enter to exit."); Console.ReadLine(); } diff --git a/Tests/Tester.Winform/TestForm.cs b/Tests/Tester.Winform/TestForm.cs index 9252774..ea5064c 100644 --- a/Tests/Tester.Winform/TestForm.cs +++ b/Tests/Tester.Winform/TestForm.cs @@ -345,7 +345,7 @@ namespace Tester.Winform { if (!_loadingCaps && _twain.State == 4) { - var sel = (int)comboDPI.SelectedItem; + var sel = (TWFix32)comboDPI.SelectedItem; _twain.CapSetDPI(sel); } }