0

I have the code below for scanning all papers. It works but the problem is when all papers will finish, it shows the error:

Exception User-Unhandeled System.runtime.InterpService.COMEException: Exception From HRESULT:0*80210003

which belongs to this line:

var imgFile = (ImageFile)ScanerItem.Transfer();

How can i fix it? Here is the code:

        public async void button1_Click(object sender, EventArgs e)
        {

                try
                {

                    object[] items = await Task.Run<object[]>(() =>
                    {

                        var deviceManager = new DeviceManager();
                        List<object> result = new List<object>();


                        for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++) // Loop Through the get List Of Devices.
                        {
                            if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
                            {
                                continue;
                            }
                            //new
                            result.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
                        }
                        return result.ToArray();


                    });

                    foreach (var item in items)
                    {
                        lstListOfScanner.Items.Add(item);
                    }

            }
                catch (COMException ex)
                {
                    MessageBox.Show(ex.Message);
                }    
bool continueScanning2 = true;
    try
    {
        await Task.Run(() =>
        {
            var deviceManager = new DeviceManager();
            DeviceInfo AvailableScanner = null;
            while (continueScanning2)
            {

                string name2 = Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
                var Path = @"C:\Users\...\Desktop\test\" + name2; // save the image in some path with filename.
                pictureBox1.ImageLocation = Path;
                bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        });
    }
    catch (COMException ex)
    {
        MessageBox.Show(ex.Message);
    }
Flash
  • 85
  • 10

1 Answers1

1

The description for the error code provided on this microsoft page says

WIA_ERROR_PAPER_EMPTY

There are no documents in the document feeder.

And you confirmed that the error is shown when all papers are scanned.

In the catch block you can check the error code and handle the case with the WIA_ERROR_PAPER_EMPTY error encountered in a way you find appropriate:

catch (COMException ex)
{
    uint errorCode = (uint)ex.ErrorCode;
    if (errorCode == 0x80210003)
    {
        // handle "There are no documents in the document feeder"
    }
    else
    {
        MessageBox.Show(ex.Message);
    }
}
Community
  • 1
  • 1
max
  • 466
  • 3
  • 7
  • 1
    @arash6657, indeed, I've ventured into some online resources regarding the WIA service and realized that the indexing you provided is correct. Unfortunately, I didn't find any way to check if the scanning process is finished, so handling the exception and checking the error code seems like a possible solution to me, though I believe there should be other options. – max Nov 19 '19 at 06:02
  • Thanks.It's somw how good but generally we need to see a warning if there is no paper in scanner. Otherwise even i don't put any paper into scanner and i press the button, i will not informed. If it helps, in the code which i had before, it workd well but the problem with that code was: the windows freezes during working (you also did favour and modified that). could you please see the previous code on the link below to see if there is any clue to fix it? Thanks. https://stackoverflow.com/questions/58873895/how-to-use-thread-to-release-the-application-from-freezing-in-c-sharp – Flash Nov 19 '19 at 08:20