AcquireModal in a Thread

Questions, comments and suggestions concerning VintaSoft Twain .NET SDK.

Moderator: Alex

Post Reply
Aaron Jones
Posts: 5
Joined: Wed Sep 07, 2011 5:58 pm

AcquireModal in a Thread

Post 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?
Alex
Site Admin
Posts: 2362
Joined: Thu Jul 10, 2008 2:21 pm

Re: AcquireModal in a Thread

Post 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
Aaron Jones
Posts: 5
Joined: Wed Sep 07, 2011 5:58 pm

Re: AcquireModal in a Thread

Post by Aaron Jones »

Thanks.
I moved the device.open to the thread and now all works as it should.
Post Reply