Page 1 of 1

AcquireModal in a Thread

Posted: Wed Sep 07, 2011 6:21 pm
by Aaron Jones
Scanner: Canon DR-2050C
OS: Windows 7 64-bit

I'm currently using the evaluation version of VintaSoftTwain.NET version 8.0.0.9

I am creating a thread with an AcqiureModal loop inside of that thread.

The problem is that the first call to AcquireModal runs on the main thread despite being called from inside of a different thread.
All other calls to AcquireModal after the first one execute in the thread that they are called from.
This causes the windows form to freeze until the after first page finishes scanning at which point any further pages being scanned do not freeze the windows form.

Code Snippet:

Code: Select all

Private Sub BeginAquire(ByVal pDevice As Vintasoft.Twain.Device)
     Dim trdAcquire As System.Threading.Thread = New System.Threading.Thread(AddressOf AcquireThread)
     trdAcquire.Start(pDevice)
End Sub

Private Sub AcquireThread(ByVal pDevice As Object)
     Dim Dev As Vintasoft.Twain.Device = DirectCast(pDevice, Vintasoft.Twain.Device)
     Dim ModalState As Vintasoft.Twain.AcquireModalState = AcquireModalState.None
     Do
          ModalState = Dev.AcquireModal() ' First time through the loop, this is run on Main Thread
          Select Case ModalState
               Case AcquireModalState.ImageAcquiring
                    While Dev.AcquiredImages.Count = 0
                    End While
                    SaveImage(Dev.AcquiredImages.Last)
               Case AcquireModalState.ImageAcquired
                    SaveImage(Dev.AcquiredImages.Last)
               Case AcquireModalState.ScanCompleted
                    _device_ScanCompleted(Nothing, Nothing)
               Case AcquireModalState.ScanCanceled
                    If Dev.State = DeviceState.Opened Then
                         ' close the device
                         Dev.Close()
                    End If
               Case AcquireModalState.ScanFailed
                    If Dev.State = DeviceState.Opened Then
                         ' close the device
                         Dev.Close()
                    End If
          End Select
     Loop While ModalState <> AcquireModalState.None
End Sub
My question is, will this continue to happen in the registered version, or does this only happen because the first call to AcquireModal is popping up the window with the "Evaluate" and "Register Now" buttons?

Re: AcquireModal in a Thread

Posted: Thu Sep 08, 2011 10:35 am
by Alex
Hello Aaron,

You have problems because you have create the object of the DeviceManager class in the main thread but use methods of object in the background thread. Please move initialization of the DeviceManager object into your background thread and you will have no problem.

Best regards, Alexander

Re: AcquireModal in a Thread

Posted: Thu Sep 08, 2011 5:44 pm
by Aaron Jones
Thanks.
I moved the device.open to the thread and now all works as it should.