Search code examples
vb.nettwain

How to close device in Twain UI


I am having the current problem. Using the Twain UI if I click on cancel it is not going to the catch exception and is not closing the device. If I once more open the UI and click on scan it does not let me open the device it stays closed with the current exception of: Device is not open Error code: 11.

    Public Sub AcquireImage()
    OpenDeviceManager()
    Dim device As Vintasoft.Twain.Device = devManager.Devices.Find("KODAK Scanner: i1150/i1180")

    Try
        snCount = 0
        imgCount = 0

        device.ShowUI = True
        device.Open() ''PROBLEM IS HERE


        device.PixelType = PixelType.Gray
        device.Acquire()

        AddHandler device.ImageAcquired, AddressOf device_ImageAcquired
        AddHandler device.ScanCompleted, AddressOf device_ScanCompleted
        AddHandler device.ScanCanceled, AddressOf device_ScanCanceled
        AddHandler device.ScanFailed, AddressOf device_ScanFailed

        device.DisableAfterAcquire = True


    Catch ex As TwainException
        MessageBox.Show(ex.Message) '' Device is not opened Error code: 11
        CloseDevice(device)

        device.Disable()



        Return
    End Try



End Sub

enter image description here enter image description here

I can scan as many times I want but I would like to use the Cancel on the UI the device Status does not change values.


Solution

  • I solved the problem by adding the add and remove handler for device.UserInterfaceClosed.

    Private Sub AcquireImage()
        SetFormUiState(False)
        OpenDeviceManager()
        Dim device As Vintasoft.Twain.Device = devManager.Devices.Find("KODAK Scanner: i1150/i1180")
    
        Try
    
            AddHandler device.ImageAcquired, AddressOf device_ImageAcquired
            AddHandler device.ScanCompleted, AddressOf device_ScanCompleted
            AddHandler device.ScanCanceled, AddressOf device_ScanCanceled
            AddHandler device.ScanFailed, AddressOf device_ScanFailed
            **AddHandler device.UserInterfaceClosed, AddressOf device_UserInterfaceClosed**
    
            snCount = 0
            imgCount = 0
    
            device.Open()
            device.ShowUI = True
            device.PixelType = PixelType.Gray
            device.Acquire()
            device.DisableAfterAcquire = True
    
        Catch ex As TwainException
            MessageBox.Show(ex.Message)
            SetFormUiState(True)
            CloseDevice(device)
            Return
        End Try
    End Sub