All products / Saraff.TwainX.Extensions
Architecture
Saraff.TwainX.Extensions is a thin adapter over Saraff.TwainX. It does not implement TWAIN
itself; it restructures the core API into a manager/source/capability model.
Layers
flowchart LR
App["Application<br/>console / WinForms / WPF / service"] --> Dsm["XDsm<br/>manager wrapper"]
Dsm --> Ds["XDs<br/>source wrapper"]
Ds --> Cap["XCapability<T><br/>typed capability"]
Cap --> Val["XCapabilityValue<T><br/>allowed value"]
Dsm --> T["TwainX<br/>Saraff.TwainX"]
Ds --> T
Cap --> T
T --> DSM["TWAINDSM"]
DSM --> Device["DS2 data source"]
| Layer | Responsibility |
|---|---|
XDsm |
Creates or adopts TwainX, enumerates sources, owns the transfer context, disposes the session |
XDs |
Identifies one source, selects/opens it lazily, exposes capabilities and transfer helpers |
XCapability<T> |
Reads and writes one TwCap with a typed value |
XCapabilityValue<T> |
Represents one allowed value and whether it is current/default |
XCapabilitySession |
Binds capabilities to the private TwainX of an asynchronous acquisition |
TwainX |
The real TWAIN session and stream capture |
Ownership
| Object | Owns | Disposal |
|---|---|---|
XDsm |
TwainX when created by XDsm.Create(...) or when isOwns: true |
Dispose() unsubscribes, cancels async work and disposes the owned session |
XDs |
Nothing; it references XDsm |
Lightweight wrapper |
XCapability<T> |
Nothing; it references XDs or a session accessor |
No disposal |
XCapabilityValue<T> |
Nothing; it references XCapability<T> |
No disposal |
XCapabilitySession |
Nothing; it references the session TwainX |
Valid only inside the configure callback |
TwainXAcquisitionResult |
The result streams | The caller must dispose it |
XDsm.Create(twainx, isOwns: false) is the sharing path: the caller keeps ownership of the
TwainX component and disposes it separately. XDsm still subscribes to its events and
unsubscribes in Dispose().
Source selection
Only one data source is open at a time in a TwainX session. XDs.TwainX performs the switch:
- ensure the DSM is open (
XDsm.TwainXcallsOpenDSM()); - if
TwainX.SourceIndexdiffers from this wrapper’s index, callCloseDataSource(), then setSourceIndex; - call
OpenDataSource()(a no-op when the source is already open).
The getter is used by capability operations and transfer helpers, so the source is opened lazily.
Switching away from an enabled source is not allowed; the underlying TwainX throws
InvalidOperationException ("The data source is already open."). Do not switch sources while an
acquisition is running.
Session lifecycle
stateDiagram-v2
[*] --> Created: XDsm.Create()
Created --> DsmOpen: first TwainX access
DsmOpen --> SourceOpen: XDs.TwainX / capability / transfer
SourceOpen --> SourceOpen: capability operations
SourceOpen --> Transfer: transfer helper
Transfer --> SourceOpen: AcquireCompleted / AcquireError
SourceOpen --> DsmOpen: another XDs / DSM close
DsmOpen --> Disposed: XDsm.Dispose()
SourceOpen --> Disposed: XDsm.Dispose()
Transfer --> Disposed: XDsm.Dispose()
Disposed --> [*]
XDsm.Create(...)creates or adoptsTwainXand subscribes toEndXfer,XferDone,SetupMemXferEvent,MemXferEvent,SetupFileXferEvent,FileXferEvent,AcquireCompletedandAcquireError.- The first access to
XDsm.TwainXcallsTwainX.OpenDSM()and throwsInvalidOperationExceptionif the DSM cannot be opened. XDsm.DataSourcesreadsSourcesCountand yieldsXDswrappers. It does not open a source.XDs.Identity,IsDefaultandIsTwain2Compatiblequery the manager and do not open a source.XDs.TwainXselects and opens the source.XDsm.Dispose()unsubscribes from the events, clears the transfer context, cancels the asynchronous session and, when owned, closes the DSM and disposesTwainX.
Synchronous transfer context
The core TwainX raises transfer events, so the wrappers keep callbacks in an internal
per-transfer context instead of on the manager itself:
sequenceDiagram
participant App
participant Ds as XDs
participant Dsm as XDsm
participant T as TwainX
App->>Ds: NativeTransfer(result, info, complete, error)
Ds->>Dsm: BeginTransfer()
Dsm-->>Ds: XTransferContext
Ds->>Ds: store callbacks in the context
Ds->>T: Capabilities.XferMech.Set(SX_NATIVE)
Ds->>T: Acquire()
T-->>Dsm: EndXfer
Dsm-->>App: result(e)
T-->>Dsm: AcquireCompleted
Dsm->>Dsm: invoke complete, clear context
Benefits:
- a second transfer on the same
XDsmfails fast withInvalidOperationExceptioninstead of overwriting callbacks; - callbacks are released when
AcquireCompletedorAcquireErroris raised; XDsm.Dispose()can clear the context deterministically.
BeginTransfer also rejects the call while an asynchronous acquisition is active.
Asynchronous session
XDs.AcquireAsync does not reuse the XDsm session. It creates a
TwainXAcquisitionSession for the call. That session owns a dedicated thread and a private
TwainX instance:
flowchart TB
A["XDs.AcquireAsync(options, configure, ct)"] --> B["XDsm.AcquireAsync"]
B --> C["SemaphoreSlim per XDsm"]
C --> D["new TwainXAcquisitionSession(options)"]
D --> E["dedicated session thread"]
E --> F["OpenDSM + OpenDataSource"]
F --> G["switch to XDs index inside configure"]
G --> H["XCapabilitySession.GetCapability<T>"]
H --> I["Acquire"]
I --> J["TwainXAcquisitionResult"]
Consequences:
- the asynchronous path is independent of the synchronous
XDsm/XDs.TwainXstate; - only one async call runs at a time per
XDsm(the semaphore queues the next call); - synchronous and asynchronous acquisition cannot run together on the same
XDsm; - the async path uses native transfer and returns result-owned
MemoryStreamcopies, notSystem.Drawingimages; - language and country configured on
XDsm.Create(...)are not copied to the async session; useTwainXAcquireOptionsand the configure callback.
See Asynchronous acquisition for the full contract.
Error model
| Exception | Source | Meaning |
|---|---|---|
InvalidOperationException |
XDsm.TwainX |
The DSM could not be opened |
ObjectDisposedException |
XDsm.TwainX, XDs.AcquireAsync |
The manager has been disposed |
InvalidOperationException |
transfer helpers | Another transfer is already active |
InvalidOperationException |
TwainX |
The source is already open/enabled when switching |
InvalidCastException |
capability values | The TWAIN value cannot be converted to T |
OperationCanceledException |
AcquireAsync |
The token or XDsm.Dispose() cancelled the acquisition |
TwainException |
Saraff.TwainX |
A DSM or data source operation failed |