small hack to handle disablesource called from diff threads for #16

This commit is contained in:
soukoku 2014-08-31 09:01:38 -04:00
parent f073bc6b65
commit 41d821a9d5
3 changed files with 28 additions and 13 deletions

View File

@ -118,9 +118,9 @@ namespace NTwain.Internals
} while (rc == ReturnCode.Success && pending.Count != 0);
// 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
if (pending.Count == 0)
if (pending.Count == 0 && session.State > 5)
{
session.ChangeState(5, true);
session.DisableSource();

View File

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

View File

@ -41,7 +41,10 @@ namespace NTwain
_appId = appId;
((ITwainSessionInternal)this).ChangeState(1, false);
#if DEBUG
// defaults to false on release since it's only useful during dev
EnforceState = true;
#endif
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
@ -434,7 +437,7 @@ namespace NTwain
_msgLoopHook.Invoke(() =>
{
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: EnableSource.", Thread.CurrentThread.ManagedThreadId));
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: EnableSource with {1}.", Thread.CurrentThread.ManagedThreadId, mode));
// app v2.2 or higher uses callback2
if (_appId.ProtocolMajor >= 2 && _appId.ProtocolMinor >= 2)
@ -483,21 +486,32 @@ namespace NTwain
return rc;
}
bool _disabling;
ReturnCode ITwainSessionInternal.DisableSource()
{
var rc = ReturnCode.Failure;
_msgLoopHook.Invoke(() =>
if (!_disabling) // temp hack as a workaround to this being called from multiple threads (xfer logic & closedsreq msg)
{
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: DisableSource.", Thread.CurrentThread.ManagedThreadId));
rc = ((ITwainSessionInternal)this).DGControl.UserInterface.DisableDS(_twui);
if (rc == ReturnCode.Success)
_disabling = true;
try
{
_callbackObj = null;
SafeAsyncSyncableRaiseOnEvent(OnSourceDisabled, SourceDisabled);
_msgLoopHook.Invoke(() =>
{
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: DisableSource.", Thread.CurrentThread.ManagedThreadId));
rc = ((ITwainSessionInternal)this).DGControl.UserInterface.DisableDS(_twui);
if (rc == ReturnCode.Success)
{
_callbackObj = null;
SafeAsyncSyncableRaiseOnEvent(OnSourceDisabled, SourceDisabled);
}
});
}
});
finally
{
_disabling = false;
}
}
return rc;
}
@ -788,6 +802,7 @@ namespace NTwain
break;
case Message.CloseDSReq:
case Message.CloseDSOK:
Debug.WriteLine("Got msg " + msg);
// even though it says closeDS it's really disable.
// dsok is sent if source is enabled with uionly