tmyroadctfig / twaindotnet

MIT License
180 stars 117 forks source link

Error Opening Data Source #23

Open sganihar opened 9 years ago

sganihar commented 9 years ago

Hi,

I faced an issue in "TestAppWpf", Saying "error opening data source". Following are the steps to reproduce the issue:

  1. Select scanner with "ShowTainUI = true" and start scan
  2. When Scanner settings screen displays say "Cancel",
  3. try to start the scan again error pop ups: Saying "error opening data source".

If user says cancel in any stage, it should cancel the job and should be ready to re scan.

Please let me know if there is any fix for this.

Thanks and Regards, Saira

sohaiby commented 7 years ago

I am encountering the same error but in a different manner. I select the scanner and starts the scan (regardless of the scan settings). But if there's no paper in the scanner, it still executes the StartScan and OpenSource method without any exception and shows no error message. If you click on the scan button again, it shows the "Error Opening Data Source" error.

sohaiby commented 7 years ago

On debugging, I found that the condition in DataSource.cs file settings.AbortWhenNoPaperDetectable is always coming as false and hence the condition

if (settings.AbortWhenNoPaperDetectable && !PaperDetectable)
                throw new FeederEmptyException();

will never throw exception if there's no paper. Moreover, after throwing this error, it fails to close the datasource in the Close method as it tries to Disable the UI first which is returning false as TwainResult and hence it don't pass through the next if condition. I did the following workaround to get it working

  1. Replace && with || in the following condition (Open()/DataSource.cs)
    if (settings.AbortWhenNoPaperDetectable && !PaperDetectable)
                throw new FeederEmptyException();
  2. Remove the if condition in following statement (Close()/DataSource.cs)
    if (result != TwainResult.Failure)
                {
                    result = Twain32Native.DsmIdentity(
                        _applicationId,
                        IntPtr.Zero,
                        DataGroup.Control,
                        DataArgumentType.Identity,
                        Message.CloseDS,
                        SourceId);
                }
dbertolini commented 5 years ago

@sohaiby you saved my day!