diff --git a/src/NTwain/Internals/ITwainSessionInternal.cs b/src/NTwain/Internals/ITwainSessionInternal.cs
index aca2372..9655c21 100644
--- a/src/NTwain/Internals/ITwainSessionInternal.cs
+++ b/src/NTwain/Internals/ITwainSessionInternal.cs
@@ -46,6 +46,8 @@ namespace NTwain.Internals
ReturnCode EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle);
+ bool CloseDSRequested { get; }
+
///
/// Gets the triplet operations defined for audio data group.
///
diff --git a/src/NTwain/Internals/TransferLogic.cs b/src/NTwain/Internals/TransferLogic.cs
index e607882..376c34f 100644
--- a/src/NTwain/Internals/TransferLogic.cs
+++ b/src/NTwain/Internals/TransferLogic.cs
@@ -58,7 +58,7 @@ namespace NTwain.Internals
#region actually handle xfer
- if (preXferArgs.CancelAll)
+ if (preXferArgs.CancelAll || session.CloseDSRequested)
{
rc = session.DGControl.PendingXfers.Reset(pending);
}
@@ -99,21 +99,11 @@ namespace NTwain.Internals
}
}
}
-
-
- switch (rc)
- {
- case ReturnCode.Cancel:
- default:
- // as usual
- rc = session.DGControl.PendingXfers.EndXfer(pending);
- break;
- }
-
+
if (rc != ReturnCode.Success && session.StopOnTransferError)
{
// end xfer without setting rc to exit (good/bad?)
- session.DGControl.PendingXfers.EndXfer(pending);
+ session.DGControl.PendingXfers.Reset(pending);
}
else
{
@@ -122,7 +112,7 @@ namespace NTwain.Internals
}
#endregion
- } while (rc == ReturnCode.Success && pending.Count != 0);
+ } while (rc == ReturnCode.Success && pending.Count != 0 && !session.CloseDSRequested);
}
else
{
@@ -131,7 +121,8 @@ namespace NTwain.Internals
// some poorly written scanner drivers return failure on EndXfer so also check for pending count now.
// this may break with other sources but we'll see
- if (pending.Count == 0 && session.State > 5)
+ if (//pending.Count == 0 &&
+ session.State > 5)
{
session.ChangeState(5, true);
session.DisableSource();
diff --git a/src/NTwain/Properties/VersionInfo.cs b/src/NTwain/Properties/VersionInfo.cs
index eb01490..961d59c 100644
--- a/src/NTwain/Properties/VersionInfo.cs
+++ b/src/NTwain/Properties/VersionInfo.cs
@@ -23,7 +23,7 @@ namespace NTwain
///
/// The build release version number.
///
- public const string Build = "3.3.9"; // change this for each nuget release
+ public const string Build = "3.3.9.2"; // change this for each nuget release
}
diff --git a/src/NTwain/TwainSession.cs b/src/NTwain/TwainSession.cs
index 806eb61..ff70e4d 100644
--- a/src/NTwain/TwainSession.cs
+++ b/src/NTwain/TwainSession.cs
@@ -393,11 +393,30 @@ namespace NTwain
{
if (targetState < 7 && CurrentSource != null)
{
- ((ITwainSessionInternal)this).DGControl.PendingXfers.EndXfer(new TWPendingXfers());
+ // not sure if really necessary but can't hurt to pin it
+ var pending = new TWPendingXfers();
+ var handle = GCHandle.Alloc(pending, GCHandleType.Pinned);
+ try
+ {
+ ((ITwainSessionInternal)this).DGControl.PendingXfers.EndXfer(pending);
+ }
+ finally
+ {
+ handle.Free();
+ }
}
if (targetState < 6 && CurrentSource != null)
{
- ((ITwainSessionInternal)this).DGControl.PendingXfers.Reset(new TWPendingXfers());
+ var pending = new TWPendingXfers();
+ var handle = GCHandle.Alloc(pending, GCHandleType.Pinned);
+ try
+ {
+ ((ITwainSessionInternal)this).DGControl.PendingXfers.Reset(pending);
+ }
+ finally
+ {
+ handle.Free();
+ }
}
if (targetState < 5 && CurrentSource != null)
{
diff --git a/src/NTwain/TwainSessionInternal.cs b/src/NTwain/TwainSessionInternal.cs
index 493f99f..91d98fa 100644
--- a/src/NTwain/TwainSessionInternal.cs
+++ b/src/NTwain/TwainSessionInternal.cs
@@ -28,6 +28,9 @@ namespace NTwain
/// The app id.
TWIdentity ITwainSessionInternal.AppId { get { return _appId; } }
+ bool _closeRequested;
+ bool ITwainSessionInternal.CloseDSRequested { get { return _closeRequested; } }
+
void ITwainSessionInternal.UpdateCallback()
{
if (State < 4)
@@ -155,6 +158,7 @@ namespace NTwain
///
ReturnCode ITwainSessionInternal.EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle)
{
+ _closeRequested = false;
var rc = ReturnCode.Failure;
_msgLoopHook.Invoke(() =>
@@ -308,7 +312,9 @@ namespace NTwain
// some sources send this at other states so do a step down
if (State > 5)
{
- ForceStepDown(4);
+ // rather than do a close here let the transfer logic handle the close down now
+ //ForceStepDown(4);
+ _closeRequested = true;
}
else if (State == 5)
{