View on GitHub

SARAFF SOFTWARE

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&lt;T&gt;<br/>typed capability"]
    Cap --> Val["XCapabilityValue&lt;T&gt;<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:

  1. ensure the DSM is open (XDsm.TwainX calls OpenDSM());
  2. if TwainX.SourceIndex differs from this wrapper’s index, call CloseDataSource(), then set SourceIndex;
  3. 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 --> [*]
  1. XDsm.Create(...) creates or adopts TwainX and subscribes to EndXfer, XferDone, SetupMemXferEvent, MemXferEvent, SetupFileXferEvent, FileXferEvent, AcquireCompleted and AcquireError.
  2. The first access to XDsm.TwainX calls TwainX.OpenDSM() and throws InvalidOperationException if the DSM cannot be opened.
  3. XDsm.DataSources reads SourcesCount and yields XDs wrappers. It does not open a source.
  4. XDs.Identity, IsDefault and IsTwain2Compatible query the manager and do not open a source.
  5. XDs.TwainX selects and opens the source.
  6. XDsm.Dispose() unsubscribes from the events, clears the transfer context, cancels the asynchronous session and, when owned, closes the DSM and disposes TwainX.

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:

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&lt;T&gt;"]
    H --> I["Acquire"]
    I --> J["TwainXAcquisitionResult"]

Consequences:

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