More tweaks on when to disable source.

This commit is contained in:
Eugene Wang 2016-02-22 18:55:29 -05:00
parent b603dd7f59
commit 42cbd75765
5 changed files with 37 additions and 19 deletions

View File

@ -46,6 +46,8 @@ namespace NTwain.Internals
ReturnCode EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle); ReturnCode EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle);
bool CloseDSRequested { get; }
/// <summary> /// <summary>
/// Gets the triplet operations defined for audio data group. /// Gets the triplet operations defined for audio data group.
/// </summary> /// </summary>

View File

@ -58,7 +58,7 @@ namespace NTwain.Internals
#region actually handle xfer #region actually handle xfer
if (preXferArgs.CancelAll) if (preXferArgs.CancelAll || session.CloseDSRequested)
{ {
rc = session.DGControl.PendingXfers.Reset(pending); 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) if (rc != ReturnCode.Success && session.StopOnTransferError)
{ {
// end xfer without setting rc to exit (good/bad?) // end xfer without setting rc to exit (good/bad?)
session.DGControl.PendingXfers.EndXfer(pending); session.DGControl.PendingXfers.Reset(pending);
} }
else else
{ {
@ -122,7 +112,7 @@ namespace NTwain.Internals
} }
#endregion #endregion
} while (rc == ReturnCode.Success && pending.Count != 0); } while (rc == ReturnCode.Success && pending.Count != 0 && !session.CloseDSRequested);
} }
else else
{ {
@ -131,7 +121,8 @@ namespace NTwain.Internals
// some poorly written scanner drivers return failure on EndXfer so also check for pending count now. // 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 // 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.ChangeState(5, true);
session.DisableSource(); session.DisableSource();

View File

@ -23,7 +23,7 @@ namespace NTwain
/// <summary> /// <summary>
/// The build release version number. /// The build release version number.
/// </summary> /// </summary>
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
} }

View File

@ -393,11 +393,30 @@ namespace NTwain
{ {
if (targetState < 7 && CurrentSource != null) 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) 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) if (targetState < 5 && CurrentSource != null)
{ {

View File

@ -28,6 +28,9 @@ namespace NTwain
/// <value>The app id.</value> /// <value>The app id.</value>
TWIdentity ITwainSessionInternal.AppId { get { return _appId; } } TWIdentity ITwainSessionInternal.AppId { get { return _appId; } }
bool _closeRequested;
bool ITwainSessionInternal.CloseDSRequested { get { return _closeRequested; } }
void ITwainSessionInternal.UpdateCallback() void ITwainSessionInternal.UpdateCallback()
{ {
if (State < 4) if (State < 4)
@ -155,6 +158,7 @@ namespace NTwain
/// <returns></returns> /// <returns></returns>
ReturnCode ITwainSessionInternal.EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle) ReturnCode ITwainSessionInternal.EnableSource(SourceEnableMode mode, bool modal, IntPtr windowHandle)
{ {
_closeRequested = false;
var rc = ReturnCode.Failure; var rc = ReturnCode.Failure;
_msgLoopHook.Invoke(() => _msgLoopHook.Invoke(() =>
@ -308,7 +312,9 @@ namespace NTwain
// some sources send this at other states so do a step down // some sources send this at other states so do a step down
if (State > 5) 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) else if (State == 5)
{ {