Page 1 of 1

BackgroundWorker = Device is not open

Posted: Wed May 08, 2013 11:25 pm
by Poorlyte
Hi!

I'm experiencing a weird problem with a "HP Deskjet 2050 J510 TWAIN" and a backgroundworker...

When I run the "Twain Simple Demo" works fine but when I put the acquire process in a backgroundworker in the same sample project, I always receive a "Device is not open" exception.

Anyone have any idea how to solve this?

Re: BackgroundWorker = Device is not open

Posted: Thu May 09, 2013 2:00 pm
by Alex
Hello,

You have problems because you created the instance of DeviceManager class in the main thread but use methods of instance in the background thread. Please move initialization of the DeviceManager object into your background thread and you will have no problem.

Best regards, Alexander

Re: BackgroundWorker = Device is not open

Posted: Thu May 09, 2013 10:02 pm
by Poorlyte
Thanks for your help Alex!!

But I'm not using a form on the scan process. I extended a BackgroundWorker and run before the form load. Take a look at the code that i'm using. I didin't made any changes to MainForm on the sample.

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using Vintasoft.Twain;

namespace TwainSimpleDemo
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var bkg = new MyScan();
            bkg.RunWorkerAsync();
            while (bkg.IsBusy) { }

            Application.Run(new MainForm());
        }
    }

    public class MyScan : BackgroundWorker
    {
        public MyScan()
        {
            base.WorkerSupportsCancellation = true;
        }

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            List<string> devices = new List<string>();

            using (DeviceManager dm = new DeviceManager())
            {
                if (dm.IsTwainAvailable)
                {
                    dm.Open();

                    for (int i = 0; i < dm.Devices.Count; i++)
                    {
                        if (dm.Devices[i].Info.ProductName == "HP Deskjet 2050 J510 TWAIN")
                            devices.Add(dm.Devices[i].Info.ProductName);
                    }

                    if (devices.Count > 0)
                    {
                        Device device = dm.Devices.Find(devices[0]);

                        try
                        {
                            // set scan settings

                            device.TransferMode = TransferMode.Native;
                            device.ShowUI = false;
                            device.DisableAfterAcquire = true;

                            device.Open(); // ERROR HAPPENS HERE
                            device.UnitOfMeasure = UnitOfMeasure.Inches;
                            device.PixelType = PixelType.RGB;

                            // perform the synchronous image acquisition process

                            AcquireModalState acquireModalState = AcquireModalState.None;
                            do
                            {
                                acquireModalState = device.AcquireModal();
                                switch (acquireModalState)
                                {
                                    case AcquireModalState.ImageAcquiring:
                                        break;
                                    case AcquireModalState.ImageAcquired:
                                        var dummy = device.AcquiredImages.Last.GetAsBitmap(true);
                                        Application.DoEvents();
                                        break;
                                    case AcquireModalState.ScanCompleted:
                                        Debug.WriteLine("Scan Completed");
                                        break;
                                    case AcquireModalState.ScanCanceled:
                                        Debug.WriteLine("Scan Canceled");
                                        break;
                                    case AcquireModalState.ScanFailed:
                                        Debug.WriteLine("Scan Failed: " + device.ErrorString);
                                        break;
                                }
                            }
                            while (acquireModalState != AcquireModalState.None);
                        }
                        catch (TwainException ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                            CancelAsync();
                        }
                        finally
                        {
                            if (device != null)
                            {
                                if (device.State != DeviceState.Closed)
                                    device.Close();
                            }
                        }
                    }
                }
            }
        }
    }
}
PS: Sorry my bad english...

Re: BackgroundWorker = Device is not open

Posted: Mon May 13, 2013 1:31 pm
by Alex
Hello,

We have tested your code with HP ScanJet Enterprise 7000 s2 scanner and have not found any problems.

Please send me an additional information about the problem:
  • Send me working project which demonstrates the problem
  • Send me log-file generated by SDK
You can generate log-file of scanner's work as follows:
  • Enable logging (add the line below to code of your application):

    Code: Select all

    TwainEnvironment.EnableDebugging("d:\vstwain.log")
  • Run your application.
  • Send me a log-file.
Best regards, Alexander

Re: BackgroundWorker = Device is not open

Posted: Mon May 13, 2013 7:44 pm
by Poorlyte
Hi Alex!

I ran my project with debugging enabled and here is the results.

http://sdrv.ms/10Econ9

My enviroment is: Windows 7 Professional x64 and Framework 2.0.

The first log file contains the execution of the background thread and the ui (that doesn't work). The second log file contains only the execution of the ui (that works).

Thank you!

Re: BackgroundWorker = Device is not open

Posted: Mon May 20, 2013 9:33 am
by Alex
Hello,

Thank you for the project. We have tested HP LaserJet M1132 scanner with your demo and have not found any problem.

We need log-file of the TWAIN Data Source Manager for understanding the reason of problem.

Please read how to generate the log-file of the TWAIN Data Source Manager in the documentation:
http://www.vintasoft.com/docs/vstwain-d ... oting.html,

generate log-file and send it to our support team.

Best regards, Alexander

Re: BackgroundWorker = Device is not open

Posted: Tue May 21, 2013 12:19 am
by Poorlyte
Hello! Thank you for your feedback!

I made more 5 tests to get DSM and Vintasoft logs:

1.1) Using Twain 1.9 + Background Worker + UI (Doesn't work)
1.2) Using Twain 1.9 + UI (Works)
2.1) Using Twain 2.0 + Background Worker + UI (Doesn't work)
2.2) Using Twain 2.0 + UI (Works)
3.1) Using Wia Twain 1.9 + Background Worker + UI (WORKED!)
3.2) Using DSM 2.0 the Wia option is not listed.

All log files is on file "TwainTests2.zip" on my skydrive: http://sdrv.ms/10Econ9

Thank you for your help!

PS: Do you prefer I open a support ticket to track this problem?

Re: BackgroundWorker = Device is not open

Posted: Fri May 24, 2013 9:42 am
by Alex
Hello,

Thank you for log-files and sorry for the delay.

Log-files show that unknown error occurs in the device driver during opening of driver.

I think the problem in the way you are executing the thread. Also the problem may be in the device driver.

First, make sure you are using the latest driver for your scanner.

Second, test our TwainConsoleDemo with your scanner.

PS: Do you prefer I open a support ticket to track this problem?
Yes, support ticket has higher priority than topic on forum.

Best regards, Alexander