VintaSoft Twain .NET SDK v10.2
Vintasoft.Twain Namespace / Device Class / CancelTransfer Method
Example




In This Topic
    CancelTransfer Method (Device)
    In This Topic
    Sends the cancel current transfer command to the device (transfer will not be canceled right after execution of this method).
    Syntax
    'Declaration
     
    Public Sub CancelTransfer() 
    'Usage
     
    Dim instance As Device
     
    instance.CancelTransfer()
    public void CancelTransfer()
    public: void CancelTransfer(); 
    public:
    void CancelTransfer(); 
    Exceptions
    ExceptionDescription
    Thrown if the .NET debugger is used and the function evaluation requires all threads to run.
    Remarks
    Important: Transfer will not be canceled right after execution of this method, transfer will be canceled when the ScanCanceled event occurs or the AcquireModal method returns AcquireModalState.None.
    Example
    This example show how to cancel current asynchronous 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();
                }
            }
                
        }
    }
    Requirements

    Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also