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

Error Handling

Aug 6, 2014 at 12:21 PM
Hi.

First, congratulations for this great project.

I am using the DLL with many different scanners, Canon, Kodak, Brother, .... and it works perfectly.
The only problem is error handling. If there is a paper jam or if the operation is canceled or event listed on TWCC, this can not be detected in the dll.
I use _twain32.Acquire (); and never produces a TwainException and the operation ends with OK.

How I can handle errors?

Thanks and sorry for my English.
Coordinator
Aug 6, 2014 at 7:30 PM
Hello, rubgonmar.
I fixed this problem. See latest source code.
I added AcquireError event. Example of use shown below.
private void _twain32_AcquireError(object sender,Twain32.AcquireErrorEventArgs e) {
    try {
        MessageBox.Show(e.Exception.Message,e.Exception.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    } catch(Exception ex) {
        MessageBox.Show(ex.Message,ex.GetType().Name,MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
}
Aug 7, 2014 at 9:10 AM
Edited Aug 7, 2014 at 9:17 AM
Thanks for responding.

I copied the changes and the new role, but the AcquireError event never fires. I Caused a jam in the scanner, but does not enter in AcquireError.

The code I used is:
using (_twain32 = new Twain32())
                {

 _twain32.OpenDSM();
 _twain32.SourceIndex = source;
 _twain32.AcquireCompleted += new EventHandler(this._twain32_AcquireCompleted);
_twain32.TwainStateChanged += new EventHandler<Saraff.Twain.Twain32.TwainStateEventArgs>(this._twain32_TwainStateChanged);

_twain32.SetupFileXferEvent += new EventHandler<Saraff.Twain.Twain32.SetupFileXferEventArgs>(this._twain32_SetupFileXferEvent);

_twain32.FileXferEvent += new EventHandler<Saraff.Twain.Twain32.FileXferEventArgs>(this._twain32_FileXferEvent);

_twain32.DeviceEvent += new System.EventHandler<Saraff.Twain.Twain32.DeviceEventEventArgs>(this._twain32_DeviceEvent);

_twain32.EndXfer += new System.EventHandler<Saraff.Twain.Twain32.EndXferEventArgs>(this._twain32_EndXfer);
_twain32.TwainStateChanged += new System.EventHandler<Saraff.Twain.Twain32.TwainStateEventArgs>(this._twain32_TwainStateChanged);
 _twain32.AcquireError += new System.EventHandler<Saraff.Twain.Twain32.AcquireErrorEventArgs>(this._twain32_AcquireError);
                    
                    string nombre = _twain32.GetSourceProductName(_twain32.SourceIndex);
                    Boolean conectado= _twain32.OpenDataSource();
                    if (conectado == true)
                    {
                        Twain32.Enumeration _resolutions = _twain32.Capabilities.XResolution.Get();
                        Twain32.Enumeration _pixels = _twain32.Capabilities.PixelType.Get();
                        Twain32.Enumeration _xferMech = _twain32.Capabilities.XferMech.Get();
                        Twain32.Enumeration _fileFormats = _twain32.Capabilities.ImageFileFormat.Get();
                        try
                        {
                            _twain32.Capabilities.XResolution.Set((float)Convert.ToInt16(datos.settings["resolution"].ToString()));
                            _twain32.Capabilities.YResolution.Set((float)Convert.ToInt16(datos.settings["resolution"].ToString()));
                        }
                        catch (Exception exC)
                        {
                            funcG.GrabarTrace("Error: " + exC.Message.ToString());
                        }
                        try
                        {

                            if ((this._twain32.IsCapSupported(TwCap.Duplex)) != 0)
                            {
                                if ((_twain32.IsCapSupported(TwCap.DuplexEnabled) & TwQC.Set) != 0)
                                {
                                    _twain32.ResetCap(TwCap.DuplexEnabled);
                                    if (datos.settings["scanDuplex"] == "true")
                                    {
                                        _twain32.SetCap(TwCap.DuplexEnabled, true);
                                    }
                                    else
                                    {
                                        _twain32.SetCap(TwCap.DuplexEnabled, false);

                                    }
                                }

                            }
                        }
                        catch (Exception exC)
                        {
                            funcG.GrabarTrace("Error : " + exC.Message.ToString());
                        }
                       
                        if (datos.settings["color"] == "bw")
                        {
                            _twain32.Capabilities.PixelType.Set(TwPixelType.BW);
                        }
                        else
                        {
                            _twain32.Capabilities.PixelType.Set(TwPixelType.RGB);
                        }

                        _twain32.ShowUI = false;
                        
                       _twain32.Acquire();

 private void _twain32_AcquireError(object sender, Twain32.AcquireErrorEventArgs e)
        {
            try
            {
                funcG.GrabarTrace(e.Exception.Message + e.Exception.GetType().Name);
            }
            catch (Exception ex)
            {
                funcG.GrabarTrace(ex.Message + ex.GetType().Name);
            }
        }
Aug 7, 2014 at 12:46 PM
Edited Aug 7, 2014 at 12:55 PM
I've been testing also this event and it is never reached. If you disconnect the scanner while scanning the PreFilterMessage method go to this._End() and never throws the exception and therefore you never get the OnAcquireError event.

Hope this helps
Coordinator
Aug 7, 2014 at 7:12 PM
Gentlemen's, I have made ​​the necessary amendments to the code. See latest source code.
Marked as answer by SARAFF on 8/8/2014 at 5:14 AM
Aug 7, 2014 at 9:32 PM
Edited Aug 7, 2014 at 9:48 PM
Thank you very much. Tomorrow morning I'll test it.
Aug 8, 2014 at 7:26 AM
Thank you very much.
The first tests I've done, work perfectly.
Aug 8, 2014 at 12:43 PM
Thank you very much. Now it works perfectly.