VintaSoft Twain .NET SDK v10.2
In This Topic
    Pause image acquisition (stop feeder)
    In This Topic

    Many scanners have automatic document feeders. Some from these scanners allow to stop the feeder, i.e. pause the image acquisition process. Device.StopFeeder allows to stop the feeder.


    Example: Here is an example that shows how to start asynchronous image acquisition, stop the document feeder and continue asynchronous image acquisition.

    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    
    Class AsyncScan_StopFeeder
    
        Shared _isScanFinished As Boolean
        Shared _step As Integer
        Shared _imageCount As Integer
    
    
    
        ''' <summary>
        ''' Scans the first image, stops the document feeder, scans the second image.
        ''' </summary>
        Public Shared Sub ScanImage_StopFeeder_ScanImage()
            _isScanFinished = False
            _step = 0
            _imageCount = 0
    
            ' create the device manager
            Using deviceManager As New DeviceManager()
                deviceManager.IsTwain2Compatible = True
                ' open the device manager
                deviceManager.Open()
    
                ' get the device
                Dim deviceName As String = "KODAK Scanner: i5000"
                Dim device As Device = deviceManager.Devices.Find(deviceName)
                If device Is Nothing Then
                    Throw New ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
                End If
    
                ' subscribe to device events
                SubscribeToDeviceEvents(device)
    
                ' disable device UI
                device.ShowUI = False
                ' specify that device must be closed after scan
                device.DisableAfterAcquire = True
    
                ' open the device
                device.Open()
                ' specify that 2 images must be acquired from scanner
                device.XferCount = 2
    
                ' run asynchronous image acqusition
                device.Acquire()
                ' wait while feeder will be stopped
                While Not _isScanFinished
                    Application.DoEvents()
                End While
    
                If _imageCount <> 1 Then
                    ' unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device)
    
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                If device.State <> DeviceState.TransferReady Then
                    ' unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device)
    
                    Throw New ApplicationException("Feeder is NOT stopped.")
                End If
                Console.WriteLine("Feeder is stopped.")
    
                _isScanFinished = False
                ' continue asynchronous image acqusition
                device.Acquire()
                ' wait while image scan will be finished
                While Not _isScanFinished
                    Application.DoEvents()
                End While
    
                ' unsubscribe from device events
                UnsubscribeFromDeviceEvents(device)
    
                If _imageCount <> 2 Then
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                ' close the device
                device.Close()
            End Using
        End Sub
    
        ''' <summary>
        ''' Image is acquired.
        ''' </summary>
        Private Shared Sub device_ImageAcquired(ByVal sender As Object, ByVal e As ImageAcquiredEventArgs)
            Console.WriteLine("Image is acquired.")
            ' increment image count
            _imageCount += 1
    
            ' if we need stop the feeder
            If _step = 0 Then
                ' get reference to the device
                Dim device As Device = DirectCast(sender, Device)
                ' stop the feeder
                device.StopFeeder()
    
                ' specify that feeder is stopped
                _step = 1
                _isScanFinished = True
            End If
        End Sub
    
        ''' <summary>
        ''' Scan is completed.
        ''' </summary>
        Private Shared Sub device_ScanCompleted(ByVal sender As Object, ByVal e As EventArgs)
            Console.WriteLine("Scan is completed.")
        End Sub
    
        ''' <summary>
        ''' Scan is canceled.
        ''' </summary>
        Private Shared Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            Console.WriteLine("Scan is canceled.")
        End Sub
    
        ''' <summary>
        ''' Scan is failed.
        ''' </summary>
        Private Shared Sub device_ScanFailed(ByVal sender As Object, ByVal e As ScanFailedEventArgs)
            Console.WriteLine(String.Format("Scan is failed: {0}.", e.ErrorString))
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Shared Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            _isScanFinished = True
        End Sub
    
        ''' <summary>
        ''' Subscribes to the device events.
        ''' </summary>
        Private Shared Sub SubscribeToDeviceEvents(ByVal device As Device)
            AddHandler device.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            AddHandler device.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            AddHandler device.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            AddHandler device.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            AddHandler device.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
        End Sub
    
        ''' <summary>
        ''' Unsubscribes from the device events.
        ''' </summary>
        Private Shared Sub UnsubscribeFromDeviceEvents(ByVal device As Device)
            RemoveHandler device.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            RemoveHandler device.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
        End Sub
    
    End Class
                  
    
    using System;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        class AsyncScan_StopFeeder
        {
    
            static bool _isScanFinished;
            static int _step;
            static int _imageCount;
    
    
    
            /// <summary>
            /// Scans the first image, stops the document feeder, scans the second image.
            /// </summary>
            public static void ScanImage_StopFeeder_ScanImage()
            {
                _isScanFinished = false;
                _step = 0;
                _imageCount = 0;
    
                // create the device manager
                using (DeviceManager deviceManager = new DeviceManager())
                {
                    deviceManager.IsTwain2Compatible = true;
                    // open the device manager
                    deviceManager.Open();
    
                    // get the device
                    string deviceName = "KODAK Scanner: i5000";
                    Device device = deviceManager.Devices.Find(deviceName);
                    if (device == null)
                        throw new ApplicationException(string.Format("Device '{0}' is not found.", deviceName));
    
                    // subscribe to device events
                    SubscribeToDeviceEvents(device);
    
                    // disable device UI
                    device.ShowUI = false;
                    // specify that device must be closed after scan
                    device.DisableAfterAcquire = true;
    
                    // open the device
                    device.Open();
                    // specify that 2 images must be acquired from scanner
                    device.XferCount = 2;
    
                    // run asynchronous image acqusition
                    device.Acquire();
                    // wait while feeder will be stopped
                    while (!_isScanFinished)
                    {
                        Application.DoEvents();
                    }
    
                    if (_imageCount != 1)
                    {
                        // unsubscribe from device events
                        UnsubscribeFromDeviceEvents(device);
    
                        throw new ApplicationException("Wrong acquired image count.");
                    }
    
                    if (device.State != DeviceState.TransferReady)
                    {
                        // unsubscribe from device events
                        UnsubscribeFromDeviceEvents(device);
    
                        throw new ApplicationException("Feeder is NOT stopped.");
                    }
                    Console.WriteLine("Feeder is stopped.");
    
                    _isScanFinished = false;
                    // run asynchronous image acqusition
                    device.Acquire();
                    // wait while image scan will be finished
                    while (!_isScanFinished)
                    {
                        Application.DoEvents();
                    }
    
                    // unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device);
    
                    if (_imageCount != 2)
                        throw new ApplicationException("Wrong acquired image count.");
    
                    // close the device
                    device.Close();
                }
            }
    
            /// <summary>
            /// Image is acquired.
            /// </summary>
            private static void device_ImageAcquired(object sender, ImageAcquiredEventArgs e)
            {
                Console.WriteLine("Image is acquired.");
                // increment image count
                _imageCount++;
    
                // if we need stop the feeder
                if (_step == 0)
                {
                    // get reference to the device
                    Device device = (Device)sender;
                    // stop the feeder
                    device.StopFeeder();
                    
                    // specify that feeder is stopped
                    _step = 1;
                    _isScanFinished = true;
                }
            }
    
            /// <summary>
            /// Scan is completed.
            /// </summary>
            private static void device_ScanCompleted(object sender, EventArgs e)
            {
                Console.WriteLine("Scan is completed.");
            }
    
            /// <summary>
            /// Scan is canceled.
            /// </summary>
            private static void device_ScanCanceled(object sender, EventArgs e)
            {
                Console.WriteLine("Scan is canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private static void device_ScanFailed(object sender, ScanFailedEventArgs e)
            {
                Console.WriteLine(string.Format("Scan is failed: {0}.", e.ErrorString));
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            private static void device_ScanFinished(object sender, EventArgs e)
            {
                _isScanFinished = true;
            }
    
            /// <summary>
            /// Subscribes to the device events.
            /// </summary>
            private static void SubscribeToDeviceEvents(Device device)
            {
                device.ImageAcquired += new EventHandler(device_ImageAcquired);
                device.ScanCompleted += new EventHandler(device_ScanCompleted);
                device.ScanCanceled += new EventHandler(device_ScanCanceled);
                device.ScanFailed += new EventHandler(device_ScanFailed);
                device.ScanFinished += new EventHandler(device_ScanFinished);
            }
    
            /// <summary>
            /// Unsubscribes from the device events.
            /// </summary>
            private static void UnsubscribeFromDeviceEvents(Device device)
            {
                device.ImageAcquired -= new EventHandler(device_ImageAcquired);
                device.ScanCompleted -= new EventHandler(device_ScanCompleted);
                device.ScanCanceled -= new EventHandler(device_ScanCanceled);
                device.ScanFailed -= new EventHandler(device_ScanFailed);
                device.ScanFinished -= new EventHandler(device_ScanFinished);
            }
    
        }
    }
                    
    


    Example: Here is an example that shows how to start synchronous image acquisition, stop the document feeder and continue synchronous image acquisition.
    Imports Vintasoft.Twain
    
    Class SyncScan_StopFeeder
    
        ''' <summary>
        ''' This method scans the first image, stops the document feeder,
        ''' scans the second image.
        ''' </summary>
        Public Shared Sub ScanImage_StopFeeder_ScanImage()
            Dim imageCount As Integer = 0
    
            ' create the device manager
            Using deviceManager As New DeviceManager()
                deviceManager.IsTwain2Compatible = True
                ' open the device manager
                deviceManager.Open()
    
                ' get the device
                Dim deviceName As String = "KODAK Scanner: i5000"
                Dim device As Device = deviceManager.Devices.Find(deviceName)
                If device Is Nothing Then
                    Throw New ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
                End If
    
                ' disable device UI
                device.ShowUI = False
                ' specify that device must be closed after scan
                device.DisableAfterAcquire = True
    
                ' open the device
                device.Open()
                ' specify that 2 images must be acquired from scanner
                device.XferCount = 2
    
                ' run synchronous image acquisition
                Dim acquireModalState1 As AcquireModalState = AcquireModalState.None
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case AcquireModalState.ImageAcquired
                            Console.WriteLine("Image is acquired.")
                            If device.AcquiredImage IsNot Nothing Then
                                device.AcquiredImage.Dispose()
                            End If
    
                            imageCount += 1
    
                            ' stop the feeder
                            device.StopFeeder()
                            Exit Select
    
                        Case AcquireModalState.ScanCompleted
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case AcquireModalState.ScanCanceled
                            Console.WriteLine("Scan is canceled.")
                            Exit Select
    
                        Case AcquireModalState.ScanFailed
                            Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> AcquireModalState.None
    
                If imageCount <> 1 Then
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                If device.State <> DeviceState.TransferReady Then
                    Throw New ApplicationException("Feeder is NOT stopped.")
                End If
                Console.WriteLine("Feeder is stopped.")
    
                ' continue asynchronous image acquisition
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case AcquireModalState.ImageAcquired
                            Console.WriteLine("Image is acquired.")
    
                            If device.AcquiredImage IsNot Nothing Then
                                device.AcquiredImage.Dispose()
                            End If
    
                            imageCount = imageCount + 1
                            Exit Select
    
                        Case AcquireModalState.ScanCompleted
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case AcquireModalState.ScanCanceled
                            Console.WriteLine("Scan is canceled.")
                            Exit Select
    
                        Case AcquireModalState.ScanFailed
                            Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> AcquireModalState.None
    
                If imageCount <> 2 Then
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                ' close the device
                device.Close()
            End Using
        End Sub
    
    End Class
                  
    
    using System;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        class SyncScan_StopFeeder
        {
    
            /// <summary>
            /// This method scans the first image, stops the document feeder,
            /// scans the second image.
            /// </summary>
            public static void ScanImage_StopFeeder_ScanImage()
            {
                int imageCount = 0;
    
                // create the device manager
                using (DeviceManager deviceManager = new DeviceManager())
                {
                    deviceManager.IsTwain2Compatible = true;
                    // open the device manager
                    deviceManager.Open();
    
                    // get the device
                    string deviceName = "KODAK Scanner: i5000";
                    Device device = deviceManager.Devices.Find(deviceName);
                    if (device == null)
                        throw new ApplicationException(string.Format("Device '{0}' is not found.", deviceName));
    
                    // disable device UI
                    device.ShowUI = false;
                    // specify that device must be closed after scan
                    device.DisableAfterAcquire = true;
    
                    // open the device
                    device.Open();
                    // specify that 2 images must be acquired from scanner
                    device.XferCount = 2;
    
                    // run asynchronous image acquisition
                    AcquireModalState acquireModalState = AcquireModalState.None;
                    do
                    {
                        acquireModalState = device.AcquireModal();
                        switch (acquireModalState)
                        {
                            case AcquireModalState.ImageAcquired:
                                Console.WriteLine("Image is acquired.");
                                if (device.AcquiredImage != null)
                                    device.AcquiredImage.Dispose();
    
                                imageCount++;
    
                                // stop the feeder
                                device.StopFeeder();
                                break;
    
                            case AcquireModalState.ScanCompleted:
                                Console.WriteLine("Scan is completed.");
                                break;
    
                            case AcquireModalState.ScanCanceled:
                                Console.WriteLine("Scan is canceled.");
                                break;
    
                            case AcquireModalState.ScanFailed:
                                Console.WriteLine(string.Format("Scan is failed: {0}.", device.ErrorString));
                                break;
                        }
                    }
                    while (acquireModalState != AcquireModalState.None);
    
                    if (imageCount != 1)
                        throw new ApplicationException("Wrong acquired image count.");
    
                    if (device.State != DeviceState.TransferReady)
                        throw new ApplicationException("Feeder is NOT stopped.");
                    Console.WriteLine("Feeder is stopped.");
    
                    // continue asynchronous image acquisition
                    do
                    {
                        acquireModalState = device.AcquireModal();
                        switch (acquireModalState)
                        {
                            case AcquireModalState.ImageAcquired:
                                Console.WriteLine("Image is acquired.");
    
                                if (device.AcquiredImage != null)
                                    device.AcquiredImage.Dispose();
    
                                imageCount++;
                                break;
    
                            case AcquireModalState.ScanCompleted:
                                Console.WriteLine("Scan is completed.");
                                break;
    
                            case AcquireModalState.ScanCanceled:
                                Console.WriteLine("Scan is canceled.");
                                break;
    
                            case AcquireModalState.ScanFailed:
                                Console.WriteLine(string.Format("Scan is failed: {0}.", device.ErrorString));
                                break;
                        }
                    }
                    while (acquireModalState != AcquireModalState.None);
    
                    if (imageCount != 2)
                        throw new ApplicationException("Wrong acquired image count.");
    
                    // close the device
                    device.Close();
                }
            }
    
        }
    }