Cancel a twain scan programmatically

Discussions about TWAIN & WIA scanning in GdPicture.NET using GdPictureImaging.
Post Reply
Posts: 4
Joined: Mon Nov 30, 2015 7:28 am

Cancel a twain scan programmatically

Post by Yab » Wed Mar 09, 2016 8:20 am


I am evaluating the I came across this issue, I whould appreciate help.

I scan on a program that is need to be reset to its starting point after a certain time that there was no activity on the form. (It's a kiosk app so if a user left in middle of a session, we need to programmatically cancel out the unfinished work, so the next user can start from begging).

Now I call the TwainAcquireToGdPictureImage, it all work great but I try to have a timer that calls TwainCloseSource if let's say 60 secunds pessed by and the user have not scanned (the code waits for the return of TwainAcquireToGdPictureImag), when the closecource is called my program crashes if the twain is still waiting, (simply creshing, I cannot handle the error with try catch, as the bug is in the clr).

I can call closecource only when the TwainAcquireToGdPictureImage is finish, but not when it's still running.

I use vfp and gdpicture as com, I can confirm that the is crashing as I don't get any errors in vfp, I just see the .net clr.dll crash in event viewer.

I need a way of cancelling an initiated twain acquire without user input.

This is also important for other reason, as I hide the scanner ui, sometimes the ui just waits for user input and the user has not way of doing it as its hidden. But if I whould be able to cancel out the scan with a timer then this whould be solved as well for my needs.

Thank you
Please help

Calling twaindisablesource did work. For me, but still crashes when call afterwards closecource

Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Cancel a twain scan programmatically

Post by Cedric » Thu Mar 10, 2016 10:52 am


It cannot be done that easily, disabling source or simply closing it in the middle of an ongoing transfert will most probably crash and you absolutely need to know the TWAIN source state before doing anything in order to do what's right at the moment (and not only in this context, it should be the golden rule in any TWAIN application).
To know the TWAIN source state you need to use the TwainGetState method, it will return an TwainStatus enumeration member as listed here.

Now, depending on the TWAIN state there are different options:

If the state is 4 (TWAIN_SOURCE_OPEN) or 5 (TWAIN_SOURCE_ENABLED) then you may just close it without issue with the TwainCloseSource method as there is no action pending.
However if the TWAIN state is 7 (TWAIN_TRANSFERRING) or 6 (TWAIN_TRANSFER_READY), then you need to cancel the transfer first and when done, after checking the state again, you may close the source.
The TwainEndAllXfers method is probably what you need in that case as it will stop all the transfers and will return to TWAIN_SOURCE_ENABLED, please see here.

I hope this helps.

Posts: 4
Joined: Mon Nov 30, 2015 7:28 am

Re: Cancel a twain scan programmatically

Post by Yab » Fri Mar 11, 2016 5:24 am

I have tested what you suggested, I check for twainsate, I get 5 and still crashed when calling closecource.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests