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); } 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 // 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.ChangeState(5, true);
session.DisableSource(); session.DisableSource();

View File

@@ -14,6 +14,6 @@ namespace NTwain
// keep this same in majors releases // keep this same in majors releases
public const string Release = "2.0.0.0"; public const string Release = "2.0.0.0";
// change this for each nuget release // 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; _appId = appId;
((ITwainSessionInternal)this).ChangeState(1, false); ((ITwainSessionInternal)this).ChangeState(1, false);
#if DEBUG
// defaults to false on release since it's only useful during dev
EnforceState = true; EnforceState = true;
#endif
} }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
@@ -434,7 +437,7 @@ namespace NTwain
_msgLoopHook.Invoke(() => _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 // app v2.2 or higher uses callback2
if (_appId.ProtocolMajor >= 2 && _appId.ProtocolMinor >= 2) if (_appId.ProtocolMajor >= 2 && _appId.ProtocolMinor >= 2)
@@ -483,10 +486,15 @@ namespace NTwain
return rc; return rc;
} }
bool _disabling;
ReturnCode ITwainSessionInternal.DisableSource() ReturnCode ITwainSessionInternal.DisableSource()
{ {
var rc = ReturnCode.Failure; var rc = ReturnCode.Failure;
if (!_disabling) // temp hack as a workaround to this being called from multiple threads (xfer logic & closedsreq msg)
{
_disabling = true;
try
{
_msgLoopHook.Invoke(() => _msgLoopHook.Invoke(() =>
{ {
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: DisableSource.", Thread.CurrentThread.ManagedThreadId)); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: DisableSource.", Thread.CurrentThread.ManagedThreadId));
@@ -498,6 +506,12 @@ namespace NTwain
SafeAsyncSyncableRaiseOnEvent(OnSourceDisabled, SourceDisabled); SafeAsyncSyncableRaiseOnEvent(OnSourceDisabled, SourceDisabled);
} }
}); });
}
finally
{
_disabling = false;
}
}
return rc; return rc;
} }
@@ -788,6 +802,7 @@ namespace NTwain
break; break;
case Message.CloseDSReq: case Message.CloseDSReq:
case Message.CloseDSOK: case Message.CloseDSOK:
Debug.WriteLine("Got msg " + msg);
// even though it says closeDS it's really disable. // even though it says closeDS it's really disable.
// dsok is sent if source is enabled with uionly // dsok is sent if source is enabled with uionly