This project has moved. For the latest updates, please go here.

Cancel before scanning actually starts

Jan 10, 2014 at 9:54 PM
Thanks for the excellent work you have done. I was able to create an ActiveX control based on your codes. But I have one question. I create and dispose Twain32 object every time scanning operation is requested because I want to make the scanner available to other programs between operations. When ShowUI is true, the scanner dependent dialog is displayed which allows you to adjust scanning parameters. If the user hit 'Cancel' in that dialog, I want to be notified and release the scanner resource. Is there any way I can do that?
Jan 11, 2014 at 9:33 PM
Hello, tochi.
I added a new event TwainStateChanged. The code below shows how to solve your problem
        private bool _isEnable=false;

        private void _twain_TwainStateChanged(object sender,Twain32.TwainStateEventArgs e) {
            if((e.TwainState&Twain32.TwainStateFlag.DSEnabled)==0&&this._isEnable) {
                // <<< scaning finished (or closed)
Specified code is taken from Saraff.Twain.Sample1
Marked as answer by SARAFF on 6/23/2014 at 12:40 PM
Jan 13, 2014 at 7:19 PM
Thanks! It works but I still need help on releasing scanner resource. Right now I release scanner resource by the following codes.
            theScanner = null;
I am not sure if it is the correct way to do that but it works in normal scanning operations. If the scanning operation is cancelled, it does release scanner resource and make it available to other programs. But when I tried to close the Internet Explorer which contains my ActiveX control, it could not be closed unless it was killed in task manager.

In your source, I saw the codes in _OnAcquireCompleted event.
            if(this._context!=null) {
It solved the IE won't close issue after I made _context public and paste these codes in my event handler. But there should be a better way to do that. Can you show me how?
Jan 13, 2014 at 8:15 PM
Hello, tochi.
For full release the scanner resources enough following code:
Method Dispose itself closes DataSouse and DataSourceManager, if necessary.
However, to make the scanner available to other programs between operations, not necessarily release scanner resources. Moreover, several applications can simultaneously open the same DataSource (scanner). I was convinced of this on own scanner. So there is no need to close the DataSource even between scans.
P.S. If the process is closed (including emergency), all allocated resources are released automatically.
Jan 13, 2014 at 11:56 PM
Edited Jan 13, 2014 at 11:57 PM
I've tested with two Saraff.Twain.Sample1 running. The instance that first started acquire operation had no error. The later instance received an one time only warning message when starting scanning. There's no further message in the following scanning operations. The message is
Source Message
        The scanner is used by other application.
        The addition and deletion of the scan setting can not be done.

        (ID: 3176)
After clicking Ok the scanning operation started without issue. It still works but annoying. That's why I called dispose every time.

The other issue, the IE won't close issue, might only exists when the scanner resource is owned by an ActiveX inside an Internet Explorer. I'm guessing IE always tries to close ActiveX before closing its page. The ApplicationContext started by Acquire() is normally stopped by _OnAcquireCompleted event. But in the case the operation is cancelled before scanning, the ApplicationContext is still running even after theScanner.Dispose() is called. The result is that IE refuses to close the page and doesn't allow users to navigate to other pages.
Jan 14, 2014 at 1:20 PM
Hello, tochi.
This problem can be solved in two ways.
First. Implement ActiveX as shown in Saraff.Twain.Sample2. This will run the ApplicationContext only for the immediate capture of the image.
Second. Implement service of dispatcher of scan. The service should implement a queue of messages (to enable simultaneous reception of requests from different scanning applications) and scan functions.