All products / Saraff.TwainX.Extensions
Troubleshooting
No data sources
XDsm.DataSources is empty or contains only sources that cannot be opened.
- Install a TWAIN 2.x Data Source Manager (for example, the TWAIN DSM) and at least one DS2 source.
- Match the process bitness to the driver: a 32-bit DSM is invisible to a 64-bit process and vice versa.
- Check
XDsm.TwainX.SourcesCountafter the DSM is open. - TWAIN 1.x sources are not supported by
Saraff.TwainX; use theSaraff.Twainproduct instead.
InvalidOperationException: DSM don't open.
XDsm.TwainX calls OpenDSM() and throws when it returns false. Typical causes:
- no DSM is installed or it cannot be loaded;
- the DSM path is wrong;
- the process bitness does not match the DSM;
- the DSM is already open in an incompatible way.
Check the DSM installation and bitness first. If the application needs an explicit DSM path, set it
on the underlying TwainX before the first access.
TwainException: DS don't supported TWAIN 2.x specification.
Saraff.TwainX requires a DS2 data source. The source was loaded through a TWAIN 1.x DSM or is not
TWAIN 2.x compatible. Use a DS2 driver, or use the Windows Saraff.Twain product for TWAIN 1.x.
XDs.IsTwain2Compatible queries the source without opening it, so it can be used to filter the
source list.
InvalidOperationException: An acquisition is already running on this DSM.
The extensions allow only one active transfer per XDsm.
- Wait for
completeCallbackorerrorCallbackbefore starting another transfer. - Do not call a synchronous transfer while
AcquireAsyncis running. - Do not call
AcquireAsyncwhile a synchronous transfer is active. - If the previous acquisition was abandoned, dispose and recreate
XDsmrather than reusing stale transfer state.
Two AcquireAsync calls on the same XDsm are serialized: the second waits for the first. Two
synchronous calls are rejected immediately.
InvalidOperationException when switching sources
The underlying TwainX throws when SourceIndex is assigned while a data source is open and
enabled. XDs.TwainX tries to close the previous source first, but cannot close an enabled source.
Finish or cancel the current acquisition before using another XDs, or create a separate XDsm
for the other source. The TWAIN DSM is process-wide, so serializing acquisitions is still required.
ObjectDisposedException
The manager has been disposed:
XDsm.TwainXthrows afterXDsm.Dispose();XDs.AcquireAsyncthrows afterXDsm.Dispose();- a synchronous helper that reaches the disposed session fails.
Dispose XDsm only when the form/window or service is finished. Do not use a short using block
around an acquisition that continues asynchronously.
InvalidCastException from a capability
The requested T does not match the TWAIN capability type, or the value cannot be converted.
// Wrong: XResolution is a floating-point capability.
var resolution = source.GetCapability<int>(TwCap.XResolution);
// Correct:
var resolution = source.GetCapability<float>(TwCap.XResolution);
The exception message names the source and target types. Enum values can be read as numbers or strings, but an arbitrary object cannot be converted to an enum or a numeric type.
null is valid for reference types and Nullable<T>; it throws for non-nullable value types.
Async acquisition is cancelled
AcquireAsync throws OperationCanceledException/TaskCanceledException when:
- the caller’s
CancellationTokenis cancelled; XDsm.Dispose()cancels the manager lifetime token;- the source is disabled externally before any page is transferred.
Catch OperationCanceledException separately from real TWAIN errors. A pre-cancelled token fails
before any DSM or hardware access.
Async acquisition returns no pages
MaxImagesmay be0/negative (invalid) or the source may have no document in the feeder.ShowUI = falsewith a source that requires UI interaction can complete without pages.DisableAfterAcquireand the source’s own completion rules determine when the session stops.- Check
AcquireError/the task exception; the base session surfaces source errors through the task.
MaxImages must be positive when set. Use null for no limit.
Thread affinity errors
Saraff.TwainX session objects are thread-affine. Synchronous calls must run on the thread that
opened the DSM. In WinForms/WPF use the UI/STA thread and a message loop; do not call synchronous
transfer helpers from a worker thread.
XDs.AcquireAsync avoids this by creating a dedicated session thread. Its configure and progress
callbacks run on that thread. Marshal UI updates to the dispatcher/synchronization context.
Package conflicts
Do not reference both Saraff.TwainX.NET and Saraff.TwainX. Both packages ship
Saraff.TwainX.dll; duplicate assemblies cause load errors or unexpected type identities.
Saraff.Twain.Extensions is a different product. Do not mix Saraff.Twain and Saraff.TwainX
types in the same project; their Twain32/TwainX APIs and image models are not interchangeable.
Async options do not apply
XDs.AcquireAsync creates its own TwainX session. Language and Country from
XDsm.Create(...) are not copied. Use TwainXAcquireOptions for ShowUI, ModalUI, Parent,
DsmPath, MaxImages and DisableAfterAcquire, and the configure callback for capabilities.
If options is null, only ShowUI is taken from the manager’s TwainX; all other options use
their defaults.
Integration tests
Unit tests do not require a scanner. They cover conversion, enumeration, validation, transfer context guards, disposal and pre-cancelled async calls. Real acquisition needs a TWAIN 2.x DSM and a DS2 source and should be tested manually or in a dedicated hardware test environment.