VintaSoft Twain .NET SDK v10.2
In This Topic
    Cancel image acquisition
    In This Topic

    You should do the following steps if you want to cancel image acquisition:

    1. Call the Device.CancelTransfer method.
    2. Wait for the Device.ScanCanceled event if you are acquiring images asynchronously.
    3. Wait while the Device.AcquireModal method returns AcquireModalState.ScanCanceled if you are acquiring images in modal loop.
    Some high speed scanners have internal buffer and scanners cache the acquired images in the buffer if scanner is faster than application which processes acquired images. Images, which are cached in internal buffer, will be lost if you cancel the image acquisition process. You can pause image acquisition process (instead of cancelling image acquisition) if you need to keep all acquired images.


    Example: Here is an example that shows how to cancel current asynchronous image transfer.
    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    
    Class AsyncScan_Cancel
    
        Shared _isScanFinished As Boolean
        Shared _isScanCanceled As Boolean
    
    
    
        ''' <summary>
        ''' This method scans one image and cancels image scan.
        ''' </summary>
        Public Shared Sub ScanOneImageAndCancelScan()
            _isScanFinished = False
            _isScanCanceled = False
    
            ' 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
                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)
    
                ' 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 scan will be finished
                While Not _isScanFinished
                    Application.DoEvents()
                End While
    
                If Not _isScanCanceled Then
                    Throw New ApplicationException("Scan is NOT canceled.")
                End If
    
                ' unsubscribe from device events
                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)
    
                ' close the device
                device.Close()
            End Using
        End Sub
    
        Private Shared Sub device_ImageAcquired(ByVal sender As Object, ByVal e As ImageAcquiredEventArgs)
            Console.WriteLine("Image is acquired.")
            ' get reference to the device
            Dim device As Device = DirectCast(sender, Device)
            ' cancel image scan
            device.CancelTransfer()
        End Sub
    
        Private Shared Sub device_ScanCompleted(ByVal sender As Object, ByVal e As EventArgs)
            Console.WriteLine("Scan is completed.")
        End Sub
    
        Private Shared Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            _isScanCanceled = True
            Console.WriteLine("Scan is canceled.")
        End Sub
    
        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
    
        Private Shared Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            _isScanFinished = True
        End Sub
    
    End Class
                  
    
    using System;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        class AsyncScan_Cancel
        {
    
            static bool _isScanFinished;
            static bool _isScanCanceled;
    
    
    
            /// <summary>
            /// This method scans one image and cancels image scan.
            /// </summary>
            public static void ScanOneImageAndCancelScan()
            {
                _isScanFinished = false;
                _isScanCanceled = false;
    
                // 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
                    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);
    
                    // 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 scan will be finished
                    while (!_isScanFinished)
                    {
                        Application.DoEvents();
                    }
    
                    if (!_isScanCanceled)
                        throw new ApplicationException("Scan is NOT canceled.");
    
                    // unsubscribe from device events
                    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);
    
                    // close the device
                    device.Close();
                }
            }
    
            static void device_ImageAcquired(object sender, ImageAcquiredEventArgs e)
            {
                // get reference to the device
                Device device = (Device)sender;
                // cancel image scan
                device.CancelTransfer();
                Console.WriteLine("Feeder is stopped.");
            }
    
            static void device_ScanCompleted(object sender, EventArgs e)
            {
                Console.WriteLine("Scan is completed.");
            }
    
            static void device_ScanCanceled(object sender, EventArgs e)
            {
                _isScanCanceled = true;
                Console.WriteLine("Scan is canceled.");
            }
    
            static void device_ScanFailed(object sender, ScanFailedEventArgs e)
            {
                Console.WriteLine(string.Format("Scan is failed: {0}.", e.ErrorString));
            }
    
            static void device_ScanFinished(object sender, EventArgs e)
            {
                _isScanFinished = true;            
            }
    
        }
    }
                    
    


    Example: Here is an example that shows how to cancel current synchronous image transfer.
    Imports Vintasoft.Twain
    
    Class SyncScan_Cancel
    
            ''' <summary>
            ''' This method scans one image and cancels image scan.
            ''' </summary>
            Public Shared Sub ScanOneImageAndCancelScan()
                    Dim isScanCanceled As Boolean = False
    
                    ' 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 asynchronous 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
    
                            ' cancel image scan
                            device.CancelTransfer()
                            Exit Select
    
                        Case AcquireModalState.ScanCompleted
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case AcquireModalState.ScanCanceled
                            isScanCanceled = True
                            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 Not isScanCanceled Then
                                    Throw New ApplicationException("Scan is NOT canceled.")
                            End If
    
                            ' close the device
                            device.Close()
                    End Using
            End Sub
    
    End Class
                  
    
    using System;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        class SyncScan_Cancel
        {
    
            /// <summary>
            /// This method scans one image and cancels image scan.
            /// </summary>
            public static void ScanOneImageAndCancelScan()
            {
                bool isScanCanceled = false;
    
                // 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();
    
                                // cancel image scan
                                device.CancelTransfer();
                                break;
    
                            case AcquireModalState.ScanCompleted:
                                Console.WriteLine("Scan is completed.");
                                break;
    
                            case AcquireModalState.ScanCanceled:
                                isScanCanceled = true;
                                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 (!isScanCanceled)
                        throw new ApplicationException("Scan is NOT canceled.");
    
                    // close the device
                    device.Close();
                }
            }
    
        }
    }