soukoku / ntwain

A TWAIN lib for dotnet.
MIT License
119 stars 50 forks source link

TransferError event is not raised for Paperjam and other errors in source #34

Open reachgauravm opened 1 year ago

reachgauravm commented 1 year ago

We have built a custom application using NTwain to scan documents. When testing the application, we find that when an error is encountered on device side, like a paper jam, error handler event (TransferError) is raised for some scanners but it is not raised for some other scanners. Seeing this problem especially when using Canon model number, DR-G1130 and associated driver. Driver UI has been disabled by setting ShowUI and CAP_INDICATORS to false in source's capabilities. Is this an issue with NTwain or is a workaround possible to overcome this.

soukoku commented 1 year ago

Does it support CAP_DEVICEEVENT to set the ones you want to know and later can listen to in session's DeviceEvent? If it doesn't then I'm not sure how else you can know about it when it doesn't report the condition in the transfer calls like the spec says.

reachgauravm commented 1 year ago

Thanks @soukoku. We tried subscribing to CAP_DEVICEEVENT and did not see the handler getting invoked with any scanner. I am unsure if we subscribed correctly though. This is how we set the ones we are interested in handling:

var twain = new TwainSession(TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly()));
var hit = twain.FirstOrDefault();
hit.Capabilities.CapDeviceEvent.SetValue(DeviceEvent.PowerSaveNotify);
hit.Capabilities.CapDeviceEvent.SetValue(DeviceEvent.DeviceOffline);
hit.Capabilities.CapDeviceEvent.SetValue(DeviceEvent.PaperDoubleFeed);
hit.Capabilities.CapDeviceEvent.SetValue(DeviceEvent.PaperJam);

and attach a handler like this

twain.DeviceEvent += (s, e) =>
            {
                string deviceEvt = e.DeviceEvent.Event.ToString();
                PlatformInfo.Current.Log.Info("Device Event value is: " + deviceEvt);
            };

Is this the right way of implementing. I searched around but could not find a sample to set and subscribe to the device event and hence posted a code sample.

reachgauravm commented 1 year ago

@soukoku - Can you please clarify how does NTwain handle error events? For instance:

Scoria92 commented 1 year ago

Hello @soukoku , I have the same problem as @reachgauravm that no TransferErrorEvent or DeviceEvent is raised if a paper jam occurs or any other error occurs.

In my service the session is opened without a window handle so the internal message loop of NTwain is used. However the actual scanning process with twain is not executed in the main thread because the service handles another requests as well. Could this setup lead to a problem that the session is not created in the main thread and the error events cannot be raised properly?

Regards

fichtip2 commented 1 year ago

I have the same problem. In my case if there is a paper jam (or I simply open the scanner) the session gets stuck in state 6. Even session.forceStepDown does not change the state. If I try to initiate another scan it failes when trying to open the data source (return code 1 'Failure').

Sadly, the only "solution" to get it working again is to reboot the computer.